动态子类化? [英] Dynamic subclassing ?
问题描述
我有一个班级的实例,例如:
b = gtk.Button()
我会的喜欢将方法和属性添加到我的实例b中。
我知道这可能是通过黑客攻击b来实现的。使用setattr()方法。但是我会用继承来做这件事,这是一种动态子类化,
没有对类进行子类化,只有这个实例是b。 !
事实上,我的实例是b,另一类是MoreMethods
class MoreMethods:
def sayHello(个体经营):
打印你好
我怎么写......
" b = b + MoreMethods"
so" b"将继续是一个gtk.Button,+方法/属性
MoreMethods(这就是我所谓的动态继承)...所以,事情
这样应该可行:
- b.set_label(" k")
- b.sayHello()
我找不到诀窍,但我很确定它可以用一种简单的
方式。
欢迎帮助
thanx
manatlanaécrit:
我有一个班级的例子,例如:
b = gtk.Button()
我会的喜欢将方法和属性添加到我的实例b中。
我知道这可能是通过黑客攻击b来实现的。使用setattr()方法。
你甚至不需要setattr(),你可以直接设置属性。
但是我想要继承,一种动态子类化,
没有对类进行子类化,只有这个实例" b" !
事实上,我的实例是b,另一类是MoreMethods
class MoreMethods:
def sayHello(个体经营):
打印你好
我怎么写......
" b = b + MoreMethods"
so" b"将继续是一个gtk.Button,+方法/属性
MoreMethods(这就是我所谓的动态继承)...所以,事情
这样应该可行:
- b.set_label(" k")
- b.sayHello()
我找不到诀窍,但我很确定它可以以简单的方式以b $ b的方式进行。
你不一定需要在这里进行子类化。你想要的是装饰模式的典型
用例:
类MoreMethods(对象):
def __init __(自我,按钮):
self._button =按钮
def sayHello(个体经营):
print" hello"
def __getattr __(自我,姓名):
返回getattr(self._button,姓名)
def __setattr__ (自我,名称,价值):
如果dir中的名字(self._button):
setattr(self._button,name,value)
else:
object .__ setattr __(self,name,value)
b = MoreMethods(gtk.Button())
b.set_label(" k")
b.say_hello()
12 mai,17:00, Bruno Desthuilliers
< bdesth.quelquech ... @ free.quelquepart.frwrote:
manatlanaécrit:
< blockquote class =post_quotes>
我有一个类的实例,例如:
b = gtk.Button()
我想为我的实例添加方法和属性b。
我知道这可能是通过黑客攻击b使用setattr()方法。
你甚至不需要setattr(),你可以直接设置属性。
但是,我想要继承,一种动态子类化,b $ b没有对类进行子类化,只有这个实例b和b。 !
事实上,我的实例是b,另一类是MoreMethods。
class MoreMethods:
def sayHello(self):
print" hello"
你不一定需要在这里进行子类化。你想要的是装饰模式的典型
用例:
类MoreMethods(对象):
def __init __(自我,按钮):
self._button =按钮
def sayHello(个体经营):
print" hello"
def __getattr __(自我,姓名):
返回getattr(self._button,姓名)
def __setattr__ (自我,名称,价值):
如果dir中的名字(self._button):
setattr(self._button,name,value)
else:
object .__ setattr __(self,name,value)
b = MoreMethods(gtk.Button())
b.set_label(" k")
b.say_hello()
,但b除外。不再是gtk.Button,而是MoreMethods
实例......
我想要那个b保持gtk.Button ...
我有一个类的实例,例如:
>
b = gtk.Button()
我想在我的实例b中添加方法和属性。
我知道有可能通过黑客攻击b使用setattr()方法。但是我会用继承来做这件事,这是一种动态子类化,
没有对类进行子类化,只有这个实例是b。 !
事实上,我的实例是b,另一类是MoreMethods
class MoreMethods:
def sayHello(个体经营):
打印你好
我怎么写......
" b = b + MoreMethods"
so" b"将继续是一个gtk.Button,+方法/属性
MoreMethods(这就是我所谓的动态继承)...所以,事情
这样应该可行:
- b.set_label(" k")
- b.sayHello()
我找不到诀窍,但我很确定它可以以简单的方式以b $ b的方式进行。
怎么样 :
class MoreMethods:
def sayHello(个体经营):
print" hello"
class myButton(gtk.Button,MoreMethods):
pass
b = myButton()
isinstance(b,gtk.Button)#True
b.sayHello() #" hello"
Daniel
I''ve got an instance of a class, ex :
b=gtk.Button()
I''d like to add methods and attributes to my instance "b".
I know it''s possible by hacking "b" with setattr() methods. But i''d
like to do it with inheritance, a kind of "dynamic subclassing",
without subclassing the class, only this instance "b" !
In fact, i''ve got my instance "b", and another class "MoreMethods"
class MoreMethods:
def sayHello(self):
print "hello"
How could i write ...
"b = b + MoreMethods"
so "b" will continue to be a gtk.Button, + methods/attributs of
MoreMethods (it''s what i call "dynamic inheritance") ...so, things
like this should work :
- b.set_label("k")
- b.sayHello()
I can''t find the trick, but i''m pretty sure it''s possible in an easy
way.
Help is welcome
thanx
manatlan a écrit :I''ve got an instance of a class, ex :
b=gtk.Button()
I''d like to add methods and attributes to my instance "b".
I know it''s possible by hacking "b" with setattr() methods.You don''t even need setattr() here, you can set the attributes directly.
But i''d
like to do it with inheritance, a kind of "dynamic subclassing",
without subclassing the class, only this instance "b" !
In fact, i''ve got my instance "b", and another class "MoreMethods"
class MoreMethods:
def sayHello(self):
print "hello"
How could i write ...
"b = b + MoreMethods"
so "b" will continue to be a gtk.Button, + methods/attributs of
MoreMethods (it''s what i call "dynamic inheritance") ...so, things
like this should work :
- b.set_label("k")
- b.sayHello()
I can''t find the trick, but i''m pretty sure it''s possible in an easy
way.You don''t necessarily need subclassing here. What you want is a typical
use case of the Decorator pattern:
class MoreMethods(object):
def __init__(self, button):
self._button = button
def sayHello(self):
print "hello"
def __getattr__(self, name):
return getattr(self._button, name)
def __setattr__(self, name, value):
if name in dir(self._button):
setattr(self._button, name, value)
else:
object.__setattr__(self, name, value)
b = MoreMethods(gtk.Button())
b.set_label("k")
b.say_hello()
On 12 mai, 17:00, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:manatlan a écrit :
I''ve got an instance of a class, ex :
b=gtk.Button()
I''d like to add methods and attributes to my instance "b".
I know it''s possible by hacking "b" with setattr() methods.
You don''t even need setattr() here, you can set the attributes directly.
But i''d
like to do it with inheritance, a kind of "dynamic subclassing",
without subclassing the class, only this instance "b" !
In fact, i''ve got my instance "b", and another class "MoreMethods"
class MoreMethods:
def sayHello(self):
print "hello"You don''t necessarily need subclassing here. What you want is a typical
use case of the Decorator pattern:
class MoreMethods(object):
def __init__(self, button):
self._button = button
def sayHello(self):
print "hello"
def __getattr__(self, name):
return getattr(self._button, name)
def __setattr__(self, name, value):
if name in dir(self._button):
setattr(self._button, name, value)
else:
object.__setattr__(self, name, value)
b = MoreMethods(gtk.Button())
b.set_label("k")
b.say_hello()except that "b" is not anymore a "gtk.Button", but a "MoreMethods"
instance ...
i''d like that "b" stay a "gtk.Button" ...
I''ve got an instance of a class, ex :>
b=gtk.Button()
I''d like to add methods and attributes to my instance "b".
I know it''s possible by hacking "b" with setattr() methods. But i''d
like to do it with inheritance, a kind of "dynamic subclassing",
without subclassing the class, only this instance "b" !
In fact, i''ve got my instance "b", and another class "MoreMethods"
class MoreMethods:
def sayHello(self):
print "hello"
How could i write ...
"b = b + MoreMethods"
so "b" will continue to be a gtk.Button, + methods/attributs of
MoreMethods (it''s what i call "dynamic inheritance") ...so, things
like this should work :
- b.set_label("k")
- b.sayHello()
I can''t find the trick, but i''m pretty sure it''s possible in an easy
way.How about:
class MoreMethods:
def sayHello(self):
print "hello"
class myButton( gtk.Button, MoreMethods ):
pass
b = myButton( )
isinstance( b, gtk.Button ) # True
b.sayHello( ) # "hello"
Daniel
这篇关于动态子类化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!