vaultТы удивишься
это нужно делать во внутреннем методе через self.
class Foo(): def __init__(cheburashka): cheburashka.a=34 f=Foo() print f.a
FishHook
class Foo():
def __init__(cheburashka):
cheburashka.a=34
f=Foo()
print f.a
class A: def __init__( self ): self.__value = 1 def getvalue( self ): return self.__value class B( A ): def __init__( self ): A.__init__( self ) self.__value = 2 b = B() print b.getvalue() print b._B__value
vault
Покопался с метаклассами, ужаснулся, впечатлился. Статьи отличные, спасибо.
Но возник вопрос: а зачем тут, в принципе, метаклассы? Собственно, ковыряя и переиначивая решение, достиг нужного результата проще:
class Reg(object):
_instances =
def __init__(self):
self._instances.append(self)
def __iter__(self):
return iter(_instances)
fata1ex
Боюсь, нужного результаты вы не достигли.
>>> class Reg(object): ... _instances = [] ... def __init__(self): ... self._instances.append(self) ... def __iter__(self): ... return iter(self._instances) ... >>> a, b, c = Reg(), Reg(), Reg() >>> for obj in Reg: ... print obj ... ... Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: 'type' object is not iterable
>>> class Reg(object): ... _instances = [] ... def __init__(self): ... self._instances.append(self) ... def __iter__(self): ... return iter(self._instances) ... >>> a, b, c = Reg(), Reg(), Reg() >>> for obj in Reg(): ... print obj ... <__main__.Reg object at 0x8fa3d4c> <__main__.Reg object at 0x8fbbdcc> <__main__.Reg object at 0x905bf2c> <__main__.Reg object at 0x905becc>