Python Language Question: attributes of object() vs Function

后端 未结 4 1107
南方客
南方客 2020-12-14 10:02

In python, it is illegal to create new attribute for an object instance like this

>>> a = object()
>>> a.hhh = 1

throws <

4条回答
  •  醉酒成梦
    2020-12-14 10:38

    The rationale is that an instance of object() is a degenerate special case. It "is" an object but it isn't designed to be useful by itself.

    Think of object as a temporary hack, bridging old-style types and classes. In Python 3.0 it will fade into obscurity because it will no longer be used as part of

    class Foo( object ):
        pass
    
    f = Foo()
    f.randomAttribute = 3.1415926
    

提交回复
热议问题