2013-02-26 1 views
3

모든 것이 파이썬 (심지어 함수)의 객체이기 때문에 실제로 함수 클래스에서 확장 할 수 있습니까? 예를 들어 문자열 유형으로 다음을 수행 할 수 있습니다. 함수 유형에 대해 비슷한 작업을 수행 할 수있는 방법이 있습니까?파이썬에서 내장 함수 클래스를 확장 할 수 있습니까?

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

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

예를 들어 무엇을 의미하는지 명확하게 설명해 주시겠습니까? – Volatility

+0

@Volatility 나는 언어를 탐색하고있을 수 있는지 궁금해하고 있습니다. 감사. –

+1

관련 항목 : http://stackoverflow.com/questions/10061752/which-classes-cannot-be-subclassed –

답변

1

아니요. 당신은 펑터와 붙어 있습니다.

>>> 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 
+0

'types.FunctionType'을 사용하는 것이 덜 어수 선해질 수 있습니다. –

관련 문제