从__init__返回其他实例 [英] Returning other instance from __init__
问题描述
我想实现这样的事情:
class C1:
def __init __(self,xxx):
如果......:
self.foo = foo
self.bar = bar
else:
self = C1.load(xxx)
def load(xxx):
...
返回instance_of_C1
load = staticmethod(load)
这似乎不正确。我怎么能这样做?
感谢您的帮助
Paulo
< blockquote> Paulo da Silva< ps ******** @ esotericaX.ptXwrote:
我想实现类似的东西:
class C1:
def __init __(self,xxx):
如果......:
self。 foo = foo
self.bar = bar
else:
self = C1.load(xxx)
def load(xxx):
...
返回instance_of_C1
load = staticmethod(load)
这似乎不正确。我该怎么做?
出于此目的使用__new__,而不是__init__。 (你需要制作C1
newstyle,例如从object继承,制作特殊方法__new__
work)。
来自__new__你可以随意返回。但是,如果你返回
的C1实例,它将_will_传递给__init__;所以,只要确保
__init__如果通过
已经初始化的自我,就不会重做初始化。
Eg :
类C1(对象):
def __new __(cls,xxx):
如果xxx:返回类型.__ new__ (cls,xxx)
else:返回C1.load(xxx)
@staticmethod
def load(xxx):return ...无论如何......
def __init __(self,xxx):
if hasattr(self,''foo''):return
self .foo =''foo''
self.bar =''bar''
Alex
On 3月15日,06:21,a ... @ mac.com(Alex Martelli)写道:
Paulo da Silva< psdasil ... @ esotericaX.ptXwrote :
我想实现类似的东西:
class C1:
def __init __(self,xxx):
if ...:
self.foo = f oo
self.bar = bar
else:
self = C1.load(xxx)
def load(xxx):
...
return instance_of_C1
load = staticmethod(load)
>
这似乎不正确。我该怎么做?
出于此目的使用__new__,而不是__init__。 (你需要制作C1
newstyle,例如从object继承,制作特殊方法__new__
work)。
叫我一个传统主义者,但为什么工厂功能不好
足够?
def C1(xxx):
如果......:
返回the_real_C1()
否则:
返回加载(xxx)
def load(xxx):
...
返回instance_of_C1
或许看到更多特殊的方法和装饰者只会让我进入
a脾气暴躁的心情。 ;-)对我来说,Python的强大之处在于能够做出像生成callables构造函数这样的东西。虽然
提供了一些错觉,C1(在这种情况下)是一个类。
Paul
Paul Boddie< pa ** @ boddie.org.ukwrote:
...
class C1:
def __init __(self,xxx):
...
出于此目的使用__new__,而不是__init__。 (你需要制作C1
newstyle,例如从object继承,制作特殊方法__new__
work)。
叫我一个传统主义者,但为什么工厂功能不好?
足够吗?
这取决于你是否需要名字C1来引用一个类。
如果你想要名字C1可以作为一个类在这个模块之外使用(
子类,使用isinstance或issubclass,可用于IDE的
classbrowser或其他内省方法包括pydoc等),然后
使名称C1引用函数而不起作用。
或者可能看到更多特殊方法和装饰员只是让我进入了b $ ba脾气暴躁的心情。 ;-)对我来说,Python的强大之处在于能够做出像生成callables构造函数这样的东西。虽然
提供了一些错觉,C1(在这种情况下)是一个类。
对我来说,OTOH,它不只是烟雾和镜子(或者你说的是b $ b幻觉),那里'还有很多真正的力量,__ new__是
的一部分。 (装饰者只是方便的语法 - 他们让你避免
重复一个名字三次,避免重复是一件好事,
但他们真正的力量基本上是高阶函数,
当然可以与其他语法一起使用,如果需要的话)。
Alex
I would like to implement something like this:
class C1:
def __init__(self,xxx):
if ... :
self.foo = foo
self.bar = bar
else:
self=C1.load(xxx)
def load(xxx):
...
return instance_of_C1
load=staticmethod(load)
This does not seem correct. How can I do it?
Thanks for any help
Paulo
Paulo da Silva <ps********@esotericaX.ptXwrote:
I would like to implement something like this:
class C1:
def __init__(self,xxx):
if ... :
self.foo = foo
self.bar = bar
else:
self=C1.load(xxx)
def load(xxx):
...
return instance_of_C1
load=staticmethod(load)
This does not seem correct. How can I do it?Use __new__ for such purposes, not __init__. (You need to make C1
newstyle, e.g. inherit from object, to make special method __new__
work).
From __new__ you can return whatever you wish. However, if you return
an instance of C1, it _will_ be passed to __init__; so, just make sure
__init__ doesn''t redo the initialization if passed an
already-initialized self.
E.g.:
class C1(object):
def __new__(cls, xxx):
if xxx: return type.__new__(cls, xxx)
else: return C1.load(xxx)
@staticmethod
def load(xxx): return ...whatever...
def __init__(self, xxx):
if hasattr(self, ''foo''): return
self.foo = ''foo''
self.bar = ''bar''
Alex
On 15 Mar, 06:21, a...@mac.com (Alex Martelli) wrote:Paulo da Silva <psdasil...@esotericaX.ptXwrote:
I would like to implement something like this:
class C1:
def __init__(self,xxx):
if ... :
self.foo = foo
self.bar = bar
else:
self=C1.load(xxx)
def load(xxx):
...
return instance_of_C1
load=staticmethod(load)
This does not seem correct. How can I do it?
Use __new__ for such purposes, not __init__. (You need to make C1
newstyle, e.g. inherit from object, to make special method __new__
work).Call me a traditionalist, but why wouldn''t a factory function be good
enough?
def C1(xxx):
if ...:
return the_real_C1()
else:
return load(xxx)
def load(xxx):
...
return instance_of_C1
Or perhaps seeing more special methods and decorators just puts me in
a grumpy mood. ;-) For me, the power of Python is derived from being
able to do things like making callables "constructors" whilst
providing some illusion that C1 (in this case) is a class.
Paul
Paul Boddie <pa**@boddie.org.ukwrote:
...class C1:
def __init__(self,xxx):
...
Use __new__ for such purposes, not __init__. (You need to make C1
newstyle, e.g. inherit from object, to make special method __new__
work).
Call me a traditionalist, but why wouldn''t a factory function be good
enough?That depends on whether you need name C1 to refer to a class, or not.
If you want name C1 to be usable outside this module as a class (to
subclass it, use with isinstance or issubclass, be available to an IDE''s
classbrowser or other means of introspection including pydoc, etc), then
making name C1 refer to a function instead would not work.
Or perhaps seeing more special methods and decorators just puts me in
a grumpy mood. ;-) For me, the power of Python is derived from being
able to do things like making callables "constructors" whilst
providing some illusion that C1 (in this case) is a class.For me, OTOH, it''s not just smoke and mirrors (or as you say
"illusion"), there''s also plenty of real power, and __new__ is part of
that. (Decorators however are just handy syntax -- they let you avoid
repeating one name three times, and avoiding repetition is a good thing,
but their real power is essentially that of higher-order-functions that
could of course be used with other syntax if need be).
Alex
这篇关于从__init__返回其他实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!