Python:在声明阶段如何引用当前类?

问题描述 投票:0回答:1

我试图通过能够定位被“声明”而不是初始化的类来避免为AccessorFunc系统创建2个类。

现在,我必须创建2个类,在第二个类中,从第一个类扩展并定位第一个类。即:

class ExampleBase( object ):
    pass
class Example( ExampleBase ):
    ## This creates self.__a, self._a, self.a, and also self.GetA( ), self.SetA( _value ), and more...
    __a = AccessorFunc( parent = ExampleBase, key = 'a', name = 'A' );

然后将Accessor函数等添加到ExampleBase而不是Example。问题在于,__是在Example中创建的,因此,如果有人从ExampleBase创建,他们将失去对包含许多帮助器的访问器对象的引用的访问权限...

我希望能够做这样的事情:

class Example( object ):
    ## Creates self.money ( Points to getter and setter / ie: property ), self._money ( data storage, raw - same as calling self.GetMoneyRaw( ) which could return None ) and self.__money ( Accessor object reference ) -- Note: setting Default doesn't mean it will initialize this default value. The raw data can be set to None, at which point the default is used. If the value is set, then it uses that value. Values are not set if an incorrect data-type is used.
    __money = AccessorFunc( parent = this, key = 'money', name = 'Money', allowed_types = ( int, float ), default = 0.0 );

并且,虽然AccessorFunc系统的工作原理很漂亮,并且使您不必为简单的类创建数百行重复,并且还可以生成其他唯一的帮助程序,例如Has *或附加到金钱访问器的CanAfford(_x),等等。更多...

我仍然受限制,我必须创建2个对象...我必须这样做的原因__ = ...;是因为__是内部的,而我似乎无法从外部生成它。因此它成为有用的声明点,因为其余的都没有问题。

是否可以通过这种方式定位当前类?您会以某种方式认为是的,但是使用类名不起作用,因为该类尚未“存在”,但是,在构建类时,必须对正在构建的对象有一些引用...有人知道参考吗?

谢谢!

我试图通过能够针对正在“声明”而不是初始化的类来避免为AccessorFunc系统创建2个类。现在,我必须创建2个类,然后在...

python class this target self
1个回答
0
投票

您可以在定义类后始终初始化这样的属性:

© www.soinside.com 2019 - 2024. All rights reserved.