Python:从堆栈/跟踪获取类实例?

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

我正在为类的单行AccessorFunc添加实现,新系统工作正常,但如果我想访问内部函数,我必须通过自我,我宁愿不必这样做。 ..


简而言之:主要问题是:

1)如何在我试图引用的类调用的不同类中调用的方法中获取类实例引用,而不将其作为参数传递...高优先级!

2)如果我使用self.Get / self.Set / self.Del而不是lambda定义,为什么property()似乎要求更多的参数而不是必要的?得到最多应该有2个 - self和_default,Set应该有2个 - self和_value,Del应该有1个 - self,而doc是一个字符串... - 它也可能是与记忆或某些东西相关的东西构建之间的内存(我使用Sublime Text ctrl + b)... LOW PRIORITY ...我可以使用lambda ...

3)如果有人有时间在Sublime Text中查看AcecoolCodeMappingSystem中的AccessorFunc调用 - 为什么我需要定义对象中的一些函数,例如def GetLanguage(self,_default ='x'):return None - when我在init中创建了Accessor函数,我从不单独调用该类,我总是使用实例引用....一些早期使用的函数,如果没有预定义被覆盖,我得到一个Key not found or some类似的错误....低优先级(新版本没有此问题)


一旦准备就绪,我将在论坛中发布解决方案,以回应有人询问动态accessorfuncs / properties ...


例如,我这样做:

class MyClassBase:
    pass
class MyClass( MyClassBase ):
    # Note: Arg 4 is optional to set Allowed Data-Types and can be None, a type( x ) or a Dict, List or Tuple of type( x )'s which is converted to an O( 1 ) Dict of allowed data-types used to restrict the setter..
    # Note: Arg 5 is optional to set Allowed Stored Values and can be None, a single value or a Dict, List or Tuple of values which is converted to an O( 1 ) Dict of allowed values used to restrict the setter..
    # Note: I am working on adding min / max restrictions for number-based getters / setters, and callbacks used to alter the data and help in many ways...
    __Height = AccessorFuncBase( MyClassBase, 'Height', 0 )
    __Width = AccessorFuncBase( MyClassBase, 'Width', 1, ( type( 0 ), type( 0.0 ) ) )
    __Depth = AccessorFuncBase( MyClassBase, 'Depth', 2, None, ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ) )

    def __str__( self ):
        return 'Height: ' + str( self.Height ) + '\t\tWidth: ' + str( self.Width ) + '\t\tDepth: ' + str( self.Depth )

    ## Or

    def __str__( self ):
        _text = 'Height: ' + str( self.Height )
        _text += '\t\tWidth: ' + str( self.Width )
        _text += '\t\tDepth: ' + str( self.Depth )
        return _text


_class = MyClass( )
print( _class )
_class.Height = 10
_class.Width = 20
_class.Depth = 30
print( _class )
_class.Depth = 9
print( _class )

Output:
Height: 0       Width: 1        Depth: 2
Height: 10      Width: 20       Depth: 2
Height: 10      Width: 20       Depth: 9

它有效......

__Key在类中定义,因此它被设置并存在,然后AccessorFuncBase定义_Key,这是存储原始值的位置,并且还定义了属性的Key ...

几乎所有数据都是静态的 - 只有存储的值存储在MyClass实例中,其余数据存储在静态位置,这很好,因为您不需要定义最小值/最大值,默认值,允许的数据类型或价值等等......对于每一个实例 - 都没有必要......虽然价值需要不同......

无论如何,它都可以正常运行_class.Height返回Getter中的值... _class._Height(默认为None)是原始值存储在MyClass实例中的地方 - Getter返回原始值,如果设置,否则返回默认值而不设置存储值...

__Height上的数据是可以访问所有辅助函数的地方,所以如果我想将值恢复为Default,我可以调用_class .__ He​​ight.Reset(),它将原始值设置为None - 但问题是,它将不会按原样工作,我必须添加_class作为它的工作arg ...

同样,如果我想使用_class .__ He​​ight.Get(_class),_ class .__ He​​ight.Set(_class,value),或其他需要访问存储数据的函数 - 我必须添加实例MyClass引用args列表......

我想避免......

我已经尝试了很多检查元素 - 我在一个案例中使用它但是我使用了列表中的最后一个键但是最后一个键始终是本地定义的最后一个键...

我希望有一些类似于Lua的调试。*函数,它可以让你看到被调用对象/实例的引用和顺序,所以我可以避免需要向它添加实例var ...

第二个问题:当我定义属性(self.Get,self.Set,self.Del,self .__ doc)时,由于某种原因,他们期望def Set(self,_parent,_value),无论我如何定义它,除非我定义lambda函数不使用它 - 这似乎不正常 - 任何想法?

在旁注:我还尝试了一些替代方法来定义AccessorFuncs - 一些引起了一些奇怪的副作用,例如在init中定义它们导致类不知道密钥,即使在使用它们之前定义了类和右边正在使用的对象---这可以在BitBucket Acecool AcecoolCodeMappingSystem中看到---我必须定义一些空白的AccessorFuncs,例如def GetX(self):返回None并让它们覆盖,这就是为什么我正在寻找替代方案一直在研究当前的解决方案(一个使用init来定义它们,一个在定义它之后更新类,一个使用__Key = AccessorFunc.AddProperty(...)返回一个属性,以及我在这里显示的那个使用__Key = AccessorFunc(...),这是一个新的类初始化等...)...如果有人知道为什么我的动态AccessorFuncs为Sublime文本AcecoolCodeMappingSystem表现这样 - 我很想知道,特别是因为他们总是预定义bef矿石等级使用等...

python dynamic accessor
1个回答
0
投票

许多提供的答案需要每个属性有这么多行,即/和/或 - 我认为这是一个丑陋或繁琐的实现,因为多个属性需要重复性等等。我更喜欢保持沸腾状态/简化它们直到它们不能简化,或直到它没有多少用途。

简而言之:在完成的工作中,如果我重复2行代码,我通常将其转换为单行辅助函数,依此类推...我简化数学或奇数参数,如(start_x,start_y,end_x,end_y) (x,y,w,h)即x,y,x + w,y + h(有时需要min / max或者如果w / h为负且实现不喜欢它,我将从x减去y和abs w / h等。)。

覆盖内部getter / setter是一个不错的方法,但问题是你需要为每个类做这个,或者把类放到那个基础上...这对我来说不适用,因为我更喜欢自由选择子/父母继承,子节点等。

我已经创建了一个解决方案来回答问题而不使用Dict数据类型来提供数据,因为我发现输入数据很麻烦等等...

我的解决方案要求您在类上方添加2个额外的行,为要添加属性的类创建基类,然后每行1行,您可以选择添加回调来控制数据,在数据更改时通知您,限制可以根据值和/或数据类型设置的数据等等。

您还可以选择使用_object.x,_object.x = value,_object.GetX(),_ object.SetX(value),它们的处理方式相同。

此外,值是唯一分配给类实例的非静态数据,但实际属性被分配给类,这意味着您不想重复的事情,不需要重复...您可以指定一个默认值,这样每次都不需要getter,虽然有一个选项可以覆盖默认的默认值,还有另一个选项,所以getter通过覆盖默认返回来返回原始存储值(注意:这个方法)表示原始值仅在分配值时分配,否则为None - 当值为Reset时,则指定None等。)

还有许多辅助函数 - 第一个被添加的属性为类添加2个左右的助手以引用实例值...它们是重置的ResetAccessors(_key,..)varargs(所有可以使用第一个命名的args重复)和SetAccessors(_key,_value),可以选择将更多的东西添加到主类中以提高效率 - 计划的是:一种将访问者组合在一起的方法,所以如果你倾向于每次重置一些, ,您可以将它们分配给组并重置组,而不是每次都重复命名的键,等等。

实例/原始存储值存储在类_,__ class中。引用Accessor类,它包含属性的静态变量/值/函数。 _类。是在设置/获取等过程中通过实例类访问时调用的属性本身。

Accessor _class .__指向类,但因为它是内部的,所以需要在类中分配,这就是为什么我选择使用__Name = AccessorFunc(...)来分配它,每个属性有一行,有很多可选使用的参数(使用键控varargs,因为它们更容易,更有效地识别和维护)...

我还创建了许多函数,如上所述,其中一些使用访问器函数信息,因此不需要调用(因为它现在有点不方便 - 现在你需要使用_class..FunctionName(_class_instance) ,args) - 我使用堆栈/跟踪来获取实例引用以通过添加运行此位马拉松的函数来获取值,或者通过向对象添加访问器并使用self(命名为this来指出它们)来获取值用于实例并保留对self的访问,AccessorFunc类引用以及函数定义中的其他信息。

它还没有完成,但它是一个梦幻般的脚步。注意:如果不使用__Name = AccessorFunc(...)来创建属性,即使我在init函数中定义它,也无法访问__键。如果你这样做,那就没有问题。

另外:请注意,名称和密钥不同...名称是“正式”,用于创建功能名称,密钥用于数据存储和访问。即_class.x,其中小写x是键,名称将是大写X,因此GetX()是函数而不是Getx()看起来有点奇怪。这允许self.x工作并且看起来合适,但也允许GetX()并且看起来合适。

我有一个示例类设置,键/名称相同,并且显示不同。为了输出数据而创建了很多辅助函数(注意:并非所有这些都是完整的)所以你可以看到发生了什么。

使用key的当前函数列表:x,name:X输出为:

这绝不是一个全面的清单 - 有一些在发布时没有在此发表过......

_instance.SetAccessors( _key, _value [ , _key, _value ] .. )                   Instance Class Helper Function: Allows assigning many keys / values on a single line - useful for initial setup, or to minimize lines.    In short: Calls this.Set<Name>( _value ) for each _key / _value pairing.
_instance.ResetAccessors( _key [ , _key ] .. )                                 Instance Class Helper Function: Allows resetting many key stored values to None on a single line.                                           In short: Calls this.Reset<Name>() for each name provided.


Note: Functions below may list self.Get / Set / Name( _args ) - self is meant as the class instance reference in the cases below - coded as this in AccessorFuncBase Class.

this.GetX( _default_override = None, _ignore_defaults = False )                 GET:            Returns    IF ISSET: STORED_VALUE .. IF IGNORE_DEFAULTS: None  .. IF PROVIDED: DEFAULT_OVERRIDE ELSE: DEFAULT_VALUE       100
this.GetXRaw( )                                                                 RAW:            Returns    STORED_VALUE                                                                                                     100
this.IsXSet( )                                                                  ISSET:          Returns    ( STORED_VALUE != None )                                                                                         True

this.GetXToString( )                                                            GETSTR:         Returns    str( GET )                                                                                                       100
this.GetXLen( _default_override = None, _ignore_defaults = False )              LEN:            Returns    len( GET )                                                                                                       3
this.GetXLenToString( _default_override = None, _ignore_defaults = False )      LENSTR:         Returns    str( len( GET ) )                                                                                                3
this.GetXDefaultValue( )                                                        DEFAULT:        Returns    DEFAULT_VALUE                                                                                                    1111

this.GetXAccessor( )                                                            ACCESSOR:       Returns    ACCESSOR_REF ( self.__<key> )                                                                                    [ AccessorFuncBase ] Key: x : Class ID: 2231452344344 : self ID: 2231448283848        Default: 1111       Allowed Types: {"<class 'int'>": "<class 'type'>", "<class 'float'>": "<class 'type'>"}     Allowed Values: None
this.GetXAllowedTypes( )                                                        ALLOWED_TYPES:  Returns    Allowed Data-Types                                                                                               {"<class 'int'>": "<class 'type'>", "<class 'float'>": "<class 'type'>"}
this.GetXAllowedValues( )                                                       ALLOWED_VALUES: Returns    Allowed Values                                                                                                   None

this.GetXHelpers( )                                                             HELPERS:        Returns    Helper Functions String List - ie what you're reading now...                                                     THESE ROWS OF TEXT
this.GetXKeyOutput( )                                                           Returns information about this Name / Key                                                                                                   ROWS OF TEXT
this.GetXGetterOutput( )                                                        Returns information about this Name / Key                                                                                                   ROWS OF TEXT

this.SetX( _value )                                                             SET:            STORED_VALUE Setter - ie Redirect to __<Key>.Set                                                                            N / A
this.ResetX( )                                                                  RESET:          Resets STORED_VALUE to None                                                                                                 N / A

this.HasXGetterPrefix( )                                                        Returns Whether or Not this key has a Getter Prefix...                                                                                      True
this.GetXGetterPrefix( )                                                        Returns Getter Prefix...                                                                                                                    Get

this.GetXName( )                                                                Returns Accessor Name - Typically Formal / Title-Case                                                                                       X
this.GetXKey( )                                                                 Returns Accessor Property Key - Typically Lower-Case                                                                                        x
this.GetXAccessorKey( )                                                         Returns Accessor Key - This is to access internal functions, and static data...                                                             __x
this.GetXDataKey( )                                                             Returns Accessor Data-Storage Key - This is the location where the class instance value is stored..                                         _x

输出的一些数据是:

这是一个使用Demo类创建的全新类,没有分配除名称以外的任何数据(因此可以输出),这是_foo,我使用的变量名...

_foo         --- MyClass: ---- id( this.__class__ ): 2231452349064 :::: id( this ): 2231448475016

    Key       Getter Value        | Raw Key   Raw / Stored Value       | Get Default Value             Default Value            | Get Allowed Types             Allowed Types                                                              | Get Allowed Values            Allowed Values                                                                                                                                                                                                                   |

    Name:     _foo                | _Name:    _foo                     | __Name.DefaultValue( ):       AccessorFuncDemoClass    | __Name.GetAllowedTypes( )     <class 'str'>                                                              | __Name.GetAllowedValues( )    Saved Value Restrictions Levied by Data-Type                                                                                                                                                                                     |
    x:        1111                | _x:       None                     | __x.DefaultValue( ):          1111                     | __x.GetAllowedTypes( )        (<class 'int'>, <class 'float'>)                                           | __x.GetAllowedValues( )       Saved Value Restrictions Levied by Data-Type                                                                                                                                                                                     |
    y:        2222                | _y:       None                     | __y.DefaultValue( ):          2222                     | __y.GetAllowedTypes( )        (<class 'int'>, <class 'float'>)                                           | __y.GetAllowedValues( )       Saved Value Restrictions Levied by Data-Type                                                                                                                                                                                     |
    z:        3333                | _z:       None                     | __z.DefaultValue( ):          3333                     | __z.GetAllowedTypes( )        (<class 'int'>, <class 'float'>)                                           | __z.GetAllowedValues( )       Saved Value Restrictions Levied by Data-Type                                                                                                                                                                                     |
    Blah:     <class 'int'>       | _Blah:    None                     | __Blah.DefaultValue( ):       <class 'int'>            | __Blah.GetAllowedTypes( )     <class 'str'>                                                              | __Blah.GetAllowedValues( )    Saved Value Restrictions Levied by Data-Type                                                                                                                                                                                     |
    Width:    1                   | _Width:   None                     | __Width.DefaultValue( ):      1                        | __Width.GetAllowedTypes( )    (<class 'int'>, <class 'bool'>)                                            | __Width.GetAllowedValues( )   Saved Value Restrictions Levied by Data-Type                                                                                                                                                                                     |
    Height:   0                   | _Height:  None                     | __Height.DefaultValue( ):     0                        | __Height.GetAllowedTypes( )   <class 'int'>                                                              | __Height.GetAllowedValues( )  (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)                                                                                                                                                                                                   |
    Depth:    2                   | _Depth:   None                     | __Depth.DefaultValue( ):      2                        | __Depth.GetAllowedTypes( )    Saved Value Restricted to Authorized Values ONLY                           | __Depth.GetAllowedValues( )   (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)                                                                                                                                                                                                   |


this.IsNameSet( ):    True      this.GetName( ):     _foo                     this.GetNameRaw( ):    _foo                     this.GetNameDefaultValue( ):    AccessorFuncDemoClass    this.GetNameLen( ):    4    this.HasNameGetterPrefix( ):    <class 'str'>                                this.GetNameGetterPrefix( ):    None
this.IsXSet( ):       False     this.GetX( ):        1111                     this.GetXRaw( ):       None                     this.GetXDefaultValue( ):       1111                     this.GetXLen( ):       4    this.HasXGetterPrefix( ):       (<class 'int'>, <class 'float'>)             this.GetXGetterPrefix( ):       None
this.IsYSet( ):       False     this.GetY( ):        2222                     this.GetYRaw( ):       None                     this.GetYDefaultValue( ):       2222                     this.GetYLen( ):       4    this.HasYGetterPrefix( ):       (<class 'int'>, <class 'float'>)             this.GetYGetterPrefix( ):       None
this.IsZSet( ):       False     this.GetZ( ):        3333                     this.GetZRaw( ):       None                     this.GetZDefaultValue( ):       3333                     this.GetZLen( ):       4    this.HasZGetterPrefix( ):       (<class 'int'>, <class 'float'>)             this.GetZGetterPrefix( ):       None
this.IsBlahSet( ):    False     this.GetBlah( ):     <class 'int'>            this.GetBlahRaw( ):    None                     this.GetBlahDefaultValue( ):    <class 'int'>            this.GetBlahLen( ):    13   this.HasBlahGetterPrefix( ):    <class 'str'>                                this.GetBlahGetterPrefix( ):    None
this.IsWidthSet( ):   False     this.GetWidth( ):    1                        this.GetWidthRaw( ):   None                     this.GetWidthDefaultValue( ):   1                        this.GetWidthLen( ):   1    this.HasWidthGetterPrefix( ):   (<class 'int'>, <class 'bool'>)              this.GetWidthGetterPrefix( ):   None
this.IsDepthSet( ):   False     this.GetDepth( ):    2                        this.GetDepthRaw( ):   None                     this.GetDepthDefaultValue( ):   2                        this.GetDepthLen( ):   1    this.HasDepthGetterPrefix( ):   None                                         this.GetDepthGetterPrefix( ):   (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
this.IsHeightSet( ):  False     this.GetHeight( ):   0                        this.GetHeightRaw( ):  None                     this.GetHeightDefaultValue( ):  0                        this.GetHeightLen( ):  1    this.HasHeightGetterPrefix( ):  <class 'int'>                                this.GetHeightGetterPrefix( ):  (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

这是在为所有_foo属性(名称除外)分配以下相同顺序的值之后:'string',1.0,True,9,10,False

this.IsNameSet( ):    True      this.GetName( ):     _foo                     this.GetNameRaw( ):    _foo                     this.GetNameDefaultValue( ):    AccessorFuncDemoClass    this.GetNameLen( ):    4    this.HasNameGetterPrefix( ):    <class 'str'>                                this.GetNameGetterPrefix( ):    None
this.IsXSet( ):       True      this.GetX( ):        10                       this.GetXRaw( ):       10                       this.GetXDefaultValue( ):       1111                     this.GetXLen( ):       2    this.HasXGetterPrefix( ):       (<class 'int'>, <class 'float'>)             this.GetXGetterPrefix( ):       None
this.IsYSet( ):       True      this.GetY( ):        10                       this.GetYRaw( ):       10                       this.GetYDefaultValue( ):       2222                     this.GetYLen( ):       2    this.HasYGetterPrefix( ):       (<class 'int'>, <class 'float'>)             this.GetYGetterPrefix( ):       None
this.IsZSet( ):       True      this.GetZ( ):        10                       this.GetZRaw( ):       10                       this.GetZDefaultValue( ):       3333                     this.GetZLen( ):       2    this.HasZGetterPrefix( ):       (<class 'int'>, <class 'float'>)             this.GetZGetterPrefix( ):       None
this.IsBlahSet( ):    True      this.GetBlah( ):     string Blah              this.GetBlahRaw( ):    string Blah              this.GetBlahDefaultValue( ):    <class 'int'>            this.GetBlahLen( ):    11   this.HasBlahGetterPrefix( ):    <class 'str'>                                this.GetBlahGetterPrefix( ):    None
this.IsWidthSet( ):   True      this.GetWidth( ):    False                    this.GetWidthRaw( ):   False                    this.GetWidthDefaultValue( ):   1                        this.GetWidthLen( ):   5    this.HasWidthGetterPrefix( ):   (<class 'int'>, <class 'bool'>)              this.GetWidthGetterPrefix( ):   None
this.IsDepthSet( ):   True      this.GetDepth( ):    9                        this.GetDepthRaw( ):   9                        this.GetDepthDefaultValue( ):   2                        this.GetDepthLen( ):   1    this.HasDepthGetterPrefix( ):   None                                         this.GetDepthGetterPrefix( ):   (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
this.IsHeightSet( ):  True      this.GetHeight( ):   9                        this.GetHeightRaw( ):  9                        this.GetHeightDefaultValue( ):  0                        this.GetHeightLen( ):  1    this.HasHeightGetterPrefix( ):  <class 'int'>                                this.GetHeightGetterPrefix( ):  (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

_foo         --- MyClass: ---- id( this.__class__ ): 2231452349064 :::: id( this ): 2231448475016

    Key       Getter Value        | Raw Key   Raw / Stored Value       | Get Default Value             Default Value            | Get Allowed Types             Allowed Types                                                              | Get Allowed Values            Allowed Values                                                                                                                                                                                                                   |

    Name:     _foo                | _Name:    _foo                     | __Name.DefaultValue( ):       AccessorFuncDemoClass    | __Name.GetAllowedTypes( )     <class 'str'>                                                              | __Name.GetAllowedValues( )    Saved Value Restrictions Levied by Data-Type                                                                                                                                                                                     |
    x:        10                  | _x:       10                       | __x.DefaultValue( ):          1111                     | __x.GetAllowedTypes( )        (<class 'int'>, <class 'float'>)                                           | __x.GetAllowedValues( )       Saved Value Restrictions Levied by Data-Type                                                                                                                                                                                     |
    y:        10                  | _y:       10                       | __y.DefaultValue( ):          2222                     | __y.GetAllowedTypes( )        (<class 'int'>, <class 'float'>)                                           | __y.GetAllowedValues( )       Saved Value Restrictions Levied by Data-Type                                                                                                                                                                                     |
    z:        10                  | _z:       10                       | __z.DefaultValue( ):          3333                     | __z.GetAllowedTypes( )        (<class 'int'>, <class 'float'>)                                           | __z.GetAllowedValues( )       Saved Value Restrictions Levied by Data-Type                                                                                                                                                                                     |
    Blah:     string Blah         | _Blah:    string Blah              | __Blah.DefaultValue( ):       <class 'int'>            | __Blah.GetAllowedTypes( )     <class 'str'>                                                              | __Blah.GetAllowedValues( )    Saved Value Restrictions Levied by Data-Type                                                                                                                                                                                     |
    Width:    False               | _Width:   False                    | __Width.DefaultValue( ):      1                        | __Width.GetAllowedTypes( )    (<class 'int'>, <class 'bool'>)                                            | __Width.GetAllowedValues( )   Saved Value Restrictions Levied by Data-Type                                                                                                                                                                                     |
    Height:   9                   | _Height:  9                        | __Height.DefaultValue( ):     0                        | __Height.GetAllowedTypes( )   <class 'int'>                                                              | __Height.GetAllowedValues( )  (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)                                                                                                                                                                                                   |
    Depth:    9                   | _Depth:   9                        | __Depth.DefaultValue( ):      2                        | __Depth.GetAllowedTypes( )    Saved Value Restricted to Authorized Values ONLY                           | __Depth.GetAllowedValues( )   (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)                                                                                                                                                                                                   |

请注意,由于受限制的数据类型或值限制,未分配某些数据 - 这是设计使然。设置器禁止分配错误的数据类型或值,即使被指定为默认值(除非您覆盖默认值保护行为)

代码还没有在这里发布,因为我在示例和解释之后没有空间......还因为它会改变。

请注意:在发布时,文件很乱 - 这会改变。但是,如果你在Sublime Text中运行它并编译它,或者从Python运行它,它将编译并吐出大量信息 - AccessorDB部分没有完成(它将用于更新Print Getters和GetKeyOutput助手)函数以及更改为实例函数,可能放入单个函数并重命名 - 查找它..)

下一步:它不是运行所需的一切 - 底部的许多注释内容是用于调试的更多信息 - 下载时它可能不存在。如果是,您应该能够取消注释并重新编译以获取更多信息。

我正在寻找需要MyClassBase的解决方法:pass,MyClass(MyClassBase):... - 如果你知道一个解决方案 - 发布它。

这个类中唯一需要的是__行 - str用于调试,就像init一样 - 它们可以从Demo Class中删除,但你需要注释掉或删除下面的一些行(_foo / 2/3 )..

顶部的String,Dict和Util类是我的Python库的一部分 - 它们并不完整。我从库中复制了一些我需要的东西,然后创建了一些新的东西。完整的代码将链接到完整的库,并将包括它以及提供更新的调用和删除代码(实际上,唯一剩下的代码将是Demo Class和print语句 - AccessorFunc系统将被移动到库中)。 ..

部分文件:

##
## MyClass Test AccessorFunc Implementation for Dynamic 1-line Parameters
##
class AccessorFuncDemoClassBase( ):
    pass
class AccessorFuncDemoClass( AccessorFuncDemoClassBase ):
    __Name      = AccessorFuncBase( parent = AccessorFuncDemoClassBase, name = 'Name',      default = 'AccessorFuncDemoClass',  allowed_types = ( TYPE_STRING ),                    allowed_values = VALUE_ANY,                 documentation = 'Name Docs',        getter_prefix = 'Get',  key = 'Name',       allow_erroneous_default = False,    options = { } )
    __x         = AccessorFuncBase( parent = AccessorFuncDemoClassBase, name = 'X',         default = 1111,                     allowed_types = ( TYPE_INTEGER, TYPE_FLOAT ),       allowed_values = VALUE_ANY,                 documentation = 'X Docs',           getter_prefix = 'Get',  key = 'x',          allow_erroneous_default = False,    options = { } )
    __Height    = AccessorFuncBase( parent = AccessorFuncDemoClassBase, name = 'Height',    default = 0,                        allowed_types = TYPE_INTEGER,                       allowed_values = VALUE_SINGLE_DIGITS,       documentation = 'Height Docs',      getter_prefix = 'Get',  key = 'Height',     allow_erroneous_default = False,    options = { } )

这种美观使得使用AccessorFuncs / callbacks / data-type / value enforcement等动态添加属性创建新类非常容易。

目前,链接位于(此链接应反映文档的更改。):https://www.dropbox.com/s/6gzi44i7dh58v61/dynamic_properties_accessorfuncs_and_more.py?dl=0

另外:如果你不使用Sublime Text,我推荐它使用Notepad ++,Atom,Visual Code等,因为正确的线程实现使它更快,更快地使用...我也在研究类似IDE的代码它的映射系统 - 看看:https://bitbucket.org/Acecool/acecoolcodemappingsystem/src/master/(首先在软件包管理器中添加Repo,然后安装插件 - 当1.0.0版本准备就绪时,我将它添加到主插件列表中......)

我希望这个解决方案有助于......并且一如既往:

仅仅因为它有效,并没有做对 - Josh'Acocool'Moser

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