Python:从stack/trace获取类实例?

2024-10-04 01:30:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为类开发一个单行AccessorFunc添加的实现,新系统运行得很好,但是如果我想访问内部函数,我必须传递self-through到它,我不想这样做。。。你知道吗


简言之:主要问题是:

1)如何从一个方法中获取类实例引用,该方法是在另一个类中调用的,该类是由我试图引用的类调用的,而不将其作为参数传入。。。高度优先!你知道吗

2)如果我使用赛尔夫,快/ 自我设定/ 赛尔夫.德尔而不是lambda定义?Get应该最多有2个self和\u默认值,Set应该有2个self和\u值,Del应该有1个self,doc是一个字符串。。。--它也可能是一些与内存有关的东西,或者是一些在构建之间留在内存中的东西(我正在使用Sublime Text ctrl+b)。。。低优先级。。。我可以用lambda。。。你知道吗

3)如果有人有时间查看我在AcecoolCodeMappingSystem中的AccessorFunc调用(以升华文本形式)-为什么我需要在对象中定义一些函数,例如def GetLanguage(self,_default='x'):return None--当我在init中创建访问器函数时,我从不单独调用类,我总是使用实例引用。。。。有些函数是早期使用的,如果没有预定义的被覆盖,我会得到一个键找不到或一些类似的错误。。。。低优先级(新版本没有此问题)


一旦它准备好了,我将在论坛上发布解决方案,以回应有人询问有关dynamic 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

而且很有效。。。你知道吗

类中定义了\u Key,因此它被设置并存在,然后AccessorFuncBase定义了存储原始值的\u Key,还定义了属性的Key。。。你知道吗

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

不管怎样,一切都像你所看到的那样_等级。高度从Getter返回值。。。_类。\u Height(默认为None)是原始值存储在MyClass实例中的位置-Getter返回原始值(如果已设置),否则返回默认值而不设置存储值。。。你知道吗

高度处的数据是可以访问所有helper函数的地方,因此如果要将值还原为默认值,可以调用\u类__高度。重置()将原始值设置为“无”-但问题是,它不能按原样工作,我必须添加_类作为参数才能工作。。。你知道吗

同样,如果我想使用\u类__身高。快(\u类),\u类__高度.设置(\u class,value)或其他需要访问存储数据的函数-我必须将MyClass引用的实例添加到args列表中。。。你知道吗

我想避免。。。你知道吗

我尝试了很多inspect元素-我在一个例子中成功了,但是我使用了类列表中的最后一个键,但是最后一个键总是在本地定义的最后一个东西。。。你知道吗

我希望有一个类似于Lua的debug.*函数,它可以让你看到被调用对象/实例的引用和顺序,这样我就不需要添加实例变量了。。。你知道吗

第二个问题:当我定义属性(赛尔夫,快, 自我设定, 赛尔夫.德尔,self.\u doc),出于某种原因,他们期望def Set(self,\u parent,\u value),不管我如何定义它,除非我定义了不使用它的lambda函数-这看起来不正常-有什么想法吗?你知道吗

另一方面:我还尝试了一些替代方法来定义AccessorFuncs,有些方法是这样做的我发现一些奇怪的副作用,比如在init中定义它们,导致类不知道键,即使在使用它们之前定义了类,并且使用了正确的对象——这可以在BitBucket Acecool AcecoolCodeMappingSystem中看到——我不得不定义一些AccessorFuncs空白,比如def GetX(self):return None并且让它们重写,这就是为什么我正在寻找一个替代方案,并且一直在研究当前的解决方案(一个使用init来定义它们,一个在定义类之后更新它,一个使用\u Key=AccessorFunc.AddProperty属性( ... ) 它返回一个属性,我在这里展示的这个属性使用了uu Key=AccessorFunc(…)这是一个新的类初始化,等等。。。如果有人知道为什么我的动态AccessorFuncs为崇高的文本AcecoolCodeMappingSystem这样做-我很想知道,特别是因为他们总是预定义的类之前使用,等等。。。你知道吗


Tags: to实例函数selfnone高度定义def
1条回答
网友
1楼 · 发布于 2024-10-04 01:30:14

许多提供的答案要求每个属性有这么多行,即/和/或-我认为这是一个丑陋或乏味的实现,因为多个属性需要重复性,等等。我更喜欢保持沸腾/简化它们,直到它们不能再简化或直到这样做没有多大用处。你知道吗

简而言之:在已完成的工作中,如果我重复两行代码,我通常会将其转换为一行助手函数,依此类推。。。我简化了数学或奇数参数,例如(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数据类型来提供数据,因为我发现输入数据很繁琐,等等。。。你知道吗

我的解决方案要求您在类的上方额外添加两行,以便为要向其中添加属性的类创建基类,然后每行添加一行,您可以选择添加回调来控制数据、在数据更改时通知您、限制可以基于值和/或数据类型设置的数据等等。你知道吗

您还可以选择使用\u object.x,\u object.x=value_对象.GetX( ), _对象.SetX(值)并对其进行等价处理。你知道吗

此外,这些值是唯一分配给类实例的非静态数据,但实际属性分配给类意味着不想重复、不需要重复的内容。。。您可以指定一个默认值,这样getter就不需要每次都使用它,尽管有一个选项可以覆盖默认值,还有另一个选项可以让getter通过覆盖默认返回来返回原始存储值(注意:这个方法意味着只有在赋值时才指定原始值,否则为“无”-当值重置时,则为“无”赋值,以此类推你知道吗

也有许多助手函数-添加的第一个属性向类中添加了2个左右的助手,用于引用实例值。。。它们是重置访问器(\u key,…)varargs repeated(所有参数都可以使用第一个命名的args重复)和SetAccessors(\u key,\u value),并在主类中添加更多选项以提高效率-计划的方法是:将访问器分组在一起,因此如果每次都要重置一些,您可以将它们指定给一个组并重置该组,而不是每次重复指定的键,等等。你知道吗

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

访问器\u class.\u指向类,但因为它是内部的,所以需要在类中分配它,这就是为什么我选择使用\u Name=AccessorFunc(…)为了分配它,每个属性有一行代码,其中包含许多可选参数(使用键控varargs,因为它们更容易识别和维护)。。。你知道吗

如前所述,我还创建了很多函数,其中一些函数使用了访问器函数信息,因此不需要调用它(因为现在有点不方便-现在需要使用\u class..FunctionName(\u class\u instance,args)-我使用stack/trace获取实例引用来获取通过添加运行此位的函数,或通过将访问器添加到对象,并使用self(命名为this是为了指出它们用于实例,并保留对self、AccessorFunc类引用以及函数定义中的其他信息的访问)。你知道吗

虽然还没有完成,但这是一个极好的立足点。注意:如果不使用uu Name=AccessorFunc(…)要创建属性,即使我在init函数中定义了uuu键,您也不能访问它。如果你这样做了,那就没有问题了。你知道吗

另外:注意名称和键是不同的。。。名称是“formal”,用于创建函数名,密钥用于数据存储和访问。在ie\u class.x中,小写x是键,名称应该是大写x,因此GetX()是函数,而不是GetX(),这看起来有点奇怪。这允许self.x工作并看起来合适,但也允许GetX()工作并看起来合适。你知道吗

我有一个示例类,设置的key/name相同,但要显示的不同。为了输出数据而创建了许多助手函数(注意:并不是所有这些都是完整的),这样您就可以看到发生了什么。你知道吗

使用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类创建,没有任何数据分配,除了名称(因此可以输出)外,它是\u 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)

这是在以相同的顺序为所有的\u 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)                                                                                                                                                                                                   |

请注意,由于受限制的数据类型或值限制,一些数据没有被分配-这是设计的。setter禁止指定错误的数据类型或值,甚至禁止将其指定为默认值(除非重写默认值保护行为)

代码没有被张贴在这里,因为我没有空间后的例子和解释。。。也因为它会改变。你知道吗

请注意:在这个职位的时候,文件是混乱的-这将改变。但是,如果您在Sublime Text中运行它并编译它,或者从Python运行它,它将编译并吐出大量信息-AccessorDB部分没有完成(它将用于更新Print getter和GetKeyOutput helper函数,同时被更改为实例函数,可能放入单个函数并重命名-查找..)你知道吗

下一步:并不是所有的东西都需要它来运行-许多评论的东西在底部是为了更多的信息用于调试-它可能不在那里,当你下载它。如果是,您应该能够取消注释并重新编译以获得更多信息。你知道吗

我正在寻找一个需要MyClassBase的工作:pass,MyClass(MyClassBase):。。。-如果你知道一个解决方案-张贴它。你知道吗

类中唯一需要的是uuu行.str用于调试,就像init一样,它们可以从Demo类中删除,但您需要注释掉或删除下面的一些行(u foo/2/3)。。你知道吗

顶部的String、Dict和Util类是我的Python库的一部分,它们并不完整。我从图书馆复制了一些我需要的东西,然后我又创造了一些新的。完整的代码将链接到完整的库,并将包括它,同时提供更新的调用和删除代码(实际上,剩下的唯一代码将是Demo类和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等等

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

另外:如果你不使用Sublime文本,我建议你不要使用Notepad++、Atom、Visual Code等,因为正确的线程实现使得它的使用速度更快。。。我也在为它开发一个类似IDE的代码映射系统—看一下:https://bitbucket.org/Acecool/acecoolcodemappingsystem/src/master/(先在Package Manager中添加Repo,然后安装Plugin—当版本1.0.0准备好后,我会将它添加到主插件列表…)你知道吗

我希望这个解决方案有帮助。。。而且,一如既往:

仅仅因为它有效,并不意味着它是正确的-乔希'Acecool'摩瑟

相关问题 更多 >