4

すべてが Python のオブジェクト (関数も含む) であるため、関数クラスから実際に拡張することは可能ですか? たとえば、文字列型で次のことができます。関数型で同様のことを行う方法はありますか?

class Foo(str):
    def appendFoo(self):
        return self+'foo'

foo = Foo('test')
print foo
>>> test
print foo.upper()
>>> TEST
print foo.appendFoo()
>>> testfoo
4

1 に答える 1

2

いいえ、私はしたいです。あなたはファンクターで立ち往生しています。

>>> def f(): pass
...
>>> type(f)
<type 'function'>
>>> function
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
>>> function = type(f)
>>> class MyFunc(function):
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    type 'function' is not an acceptable base type
>>> type('MyFunc', (function,), {})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: type 'function' is not an acceptable base type
于 2013-02-26T05:58:06.603 に答える