为什么这么多内置类型可以继承? [英] Why are so many built-in types inheritable?
问题描述
嗨伙计们!
出于调试目的,我试过这个:
--- snip ---
def foo():传递
function = type(foo)
class PrintingFunction(function):
def __init__ (self,func):
self.func = func
def __call __(self,* args,** kwargs):
print args ,kwargs
返回函数.__调用__(self,args,kwargs)
类DebugMeta(类型):
def __new __( self,name,bases,dict):
dict中的名字:
如果type(dict [name])是函数:
dict [name] = PrintingFunction(dict [name])
--- snap ---
现在我想我能够让所有使用DebugMeta的类的方法都是
元类打印出他们的参数。但是我得到了以下可悲的错误:
TypeError:调用元类库时出错
类型''函数''不是可接受的基类型
这太糟糕了,不是吗?
我能做些什么才能让上面的代码正常工作? (不,我不喜欢在没有Python中这种令人不快的行为的情况下重新启动
工具< type''function''>。
问候,
F. Sidler
Hi folks!
For debugging purposes I tried this:
--- snip ---
def foo(): pass
function = type(foo)
class PrintingFunction(function):
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
print args, kwargs
return function.__call__(self, args, kwargs)
class DebugMeta(type):
def __new__(self, name, bases, dict):
for name in dict:
if type(dict[name]) is function:
dict[name] = PrintingFunction(dict[name])
--- snap ---
Now I tought I were able to let all maethod of classes with DebugMeta as
metaclass print out their arguments. But I got the following sad error:
TypeError: Error when calling the metaclass bases
type ''function'' is not an acceptable base type
That''s awful, isn''t it?
What could I do to get the above code working? (No, I disliked to re-
implement <type ''function''> without this unpleasant behaviour in Python.)
Greetings,
F. Sidler
推荐答案
你好?或者,有什么明显的理由让我看不到这种行为吗?
Hello? Or, is there any obvious reason for this behaviour I don''t see?
Fabiano Sidler写道:
Fabiano Sidler wrote:
嗨伙计们!
为了调试目的,我尝试了这个:
--- snip ---
def foo():传递
function = type(foo) )
类PrintingFunction(函数):
def __init __(self,func):
self.func = func
def __call __(self,* args,** kwargs):
打印args,kwargs
返回函数.__调用__(self,args,kwargs)
类DebugMeta(类型):
def __new __(self,name ,base,dict):
用于dict中的名称:
如果type(dict [name])是函数:
dict [name] = PrintingFunction(dict [name])
---快照---
现在我认为我能够让所有使用DebugMeta的类的方法打印出他们的参数。但是我得到了以下可悲的错误:
TypeError:调用元类库时出错
类型''函数''不是可接受的基类型
那个'太可怕了,不是吗?
我能做些什么来让上面的代码工作? (不,我不喜欢在Python中没有这种不愉快的行为来实现< type''function''>。)
Hi folks!
For debugging purposes I tried this:
--- snip ---
def foo(): pass
function = type(foo)
class PrintingFunction(function):
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
print args, kwargs
return function.__call__(self, args, kwargs)
class DebugMeta(type):
def __new__(self, name, bases, dict):
for name in dict:
if type(dict[name]) is function:
dict[name] = PrintingFunction(dict[name])
--- snap ---
Now I tought I were able to let all maethod of classes with DebugMeta as
metaclass print out their arguments. But I got the following sad error:
TypeError: Error when calling the metaclass bases
type ''function'' is not an acceptable base type
That''s awful, isn''t it?
What could I do to get the above code working? (No, I disliked to re-
implement <type ''function''> without this unpleasant behaviour in Python.)
你可以做这是一个简单的装饰者:
http:// wiki。 python.org/moin/PythonDe...fec0ff4b3653f4
或者我认为你的类PrintingFunction可以用作
类PrintingFunction(object):
def __init __(self,func):
self.func = func
def __call __(self,* args,** kwargs):
打印args,kwargs
返回self.func(* args,** kwargs)
Kent
You could do this with a simple decorator:
http://wiki.python.org/moin/PythonDe...fec0ff4b3653f4
or I think your class PrintingFunction would work as
class PrintingFunction(object):
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
print args, kwargs
return self.func(*args, **kwargs)
Kent
Kent Johnson< ke ** @ kentsjohnson.com>写道:
Kent Johnson <ke**@kentsjohnson.com> wrote:
你可以用一个简单的装饰器做到这一点:
http://wiki.python.org/moin/PythonDe...fec0ff4b3653f4
或者我认为您的类PrintingFunction可以作为
类工作PrintingFunction(object):
def __init __(self,func):
self.func = func
def __call __(self,* args,** kwargs):
print args, kwargs
返回self.func(* args,** kwargs)
You could do this with a simple decorator:
http://wiki.python.org/moin/PythonDe...fec0ff4b3653f4
or I think your class PrintingFunction would work as
class PrintingFunction(object):
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
print args, kwargs
return self.func(*args, **kwargs)
这个问题是func_code属性包含
是PrintingFunction的代码而不是func。我想做的是,
保持原始行为,即将变量__metaclass__设置为
DebugMeta,以便获得调试输出,而无需更改函数和
通过func_code
属性获取原始函数的代码对象,而不是PrintigFunction的属性。这就是为什么我*必须*继承
< type''function''> ;.
问候,
F. Sidler
The problem with this is that the func_code attribute would contain
the code of PrintingFunction instead of func. What I wanted to do, is
to keep the original behaviour, i.e. set the variable __metaclass__ to
DebugMeta and so get debug output, without changing a function and
getting the original function''s code object by the func_code
attribute, not PrintigFunction''s one. That''s why I *must* inherit from
<type ''function''>.
Greetings,
F. Sidler
这篇关于为什么这么多内置类型可以继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!