如何将member_descriptor绑定到类中没有引用的类.__ dict__

时间:2017-12-29 12:12:38

标签: python-3.x

由于我的私有化技巧在IDLE和python3 REPL中都有效:

>>> class A(object):
...     __slots__ = ['attr']
... 
>>> dscget = A.__dict__['attr'].__get__
>>> dscset = A.__dict__['attr'].__set__
>>> del A.attr
>>> 

但不完全在我的程序中(相同的设置和机制)

Python 3.4.3 |Anaconda 2.3.0 (32-bit)| (default, Mar  6 2015, 12:08:17) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Traceback (most recent call last):
  File "\home\tcll\Projects\python\UGE\test_FORMAT.py", line 5, in <module>
    import API
  File "\home\tcll\Projects\python\UGE\API\__init__.py", line 43, in <module>
    from . import CONST, OBJECT
  File "\home\tcll\Projects\python\UGE\API\OBJECT\__init__.py", line 191, in <module>
    from ._collection import *
  File "\home\tcll\Projects\python\UGE\API\OBJECT\_collection.py", line 209, in <module>
    private()
  File "\home\tcll\Projects\python\UGE\API\OBJECT\_collection.py", line 187, in private
    getbase,        setbase         = getset( UGECollection, '__base__' );          del UGECollection.__base__
AttributeError: readonly attribute
>>> 

我应该注意到这实际上是加载的第3个类,前2个类实际按预期工作并且加载没有问题,尽管它们不会删除属性,只会用只读属性覆盖它们。

我想知道如何初始化member_descriptor并将其注册到类中,这样我就可以在不需要类dict中的引用的情况下创建它们。

可以轻松获取member_descriptor名称,但实例创建似乎非常困难:

>>> class A(object):
...     __slots__ = ['attr']
... 
>>> member_descriptor = A.attr.__class__
>>> member_descriptor()
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    member_descriptor()
TypeError: cannot create 'member_descriptor' instances
>>> member_descriptor.__new__(member_descriptor)
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    member_descriptor.__new__(member_descriptor)
TypeError: object.__new__(member_descriptor) is not safe, use member_descriptor.__new__()
>>> 

我很确定通过python做到这一点是不可能的,但我怎么能通过像ctypes或cffi这样的东西来做呢?

1 个答案:

答案 0 :(得分:0)

好吧,在一个已定义的类之外创建一个member_descriptor是不可能的,因为yourProperty:string; getService() : void{ yourProperty = this.msalService.getProduct(); } 类基本上是C <html> <p>{{ getService() }}</p> </html> <html> <p>{{ yourProperty }}</p> </html> ,其中member_descriptor实例基本上是一个处理指针的包装器结构实例...
据我了解,您无法修改现有的结构定义...

所以我最终决定使用替代的私有化类作为存储私有属性的解决方法,如下所示:

__slots__

我发现有趣的东西...... 在大约15个课程中,我以与struct{}相同的方式私有化,def private(): privateRegistry = {} # as long as I don't need access to private attributes within __hash__ it won't infinitely recurse getprivate = privateRegistry.__getitem__; setprivate = privateRegistry.__setitem__ # just for quick access class privateAttrs(object): __slots__ = ['a'] get_a, set_a = getset( privateAttrs, 'a', privatize=False ) # just for quick access new = object.__new__ class Object(object): __slots__ = ['public'] def __new__(cls): i = new(cls) p = new(privateAttrs); setprivate( i, p ) set_a( p, 15 ) # p.a = 15 with no dot operator slowness since you're calling __set__ directly def __getitem__(i, item): p = getprivate(i) return get_a(p) return Object Object = private() del private 是唯一的罪犯...

编辑:另外,我知道这不是真正私密的,但它比UGECollection属性更好......
您可以从UGECollection中选择任何一种Object方法来访问私有属性(或本例中的私有类本身)的特定getter和setter(尽管知道它可以写入只读属性),但是由于单元格被索引而不是键入,因此我可以使用安全级别的安全级别,并且您不能期望单元格保持相同的顺序(如果有的话)有一个活跃的项目。