为什么我不能直接向任何python对象添加属性?

时间:2021-01-17 21:04:18

I have this code:

我有这个代码:

>>> class G:
...   def __init__(self):
...     self.x = 20
...
>>> gg = G()
>>> gg.x
20
>>> gg.y = 2000

And this code:

这段代码:

>>> from datetime import datetime
>>> my_obj = datetime.now()
>>> my_obj.interesting = 1
*** AttributeError: 'datetime.datetime' object has no attribute 'interesting'

From my Python knowledge, I would say that datetime overrides setattr/getattr, but I am not sure. Could you shed some light here?

根据我的Python知识,我会说datetime会覆盖setattr / getattr,但我不确定。你能在这里说清楚吗?

EDIT: I'm not specifically interested in datetime. I was wondering about objects in general.

编辑:我对datetime并不特别感兴趣。我一般都想知道对象。

3 个解决方案

#1


My guess, is that the implementation of datetime uses __slots__ for better performance.

我的猜测是,datetime的实现使用__slots__以获得更好的性能。

When using __slots__, the interpreter reserves storage for just the attributes listed, nothing else. This gives better performance and uses less storage, but it also means you can't add new attributes at will.

使用__slots__时,解释器仅为列出的属性保留存储空间,而不保留其他内容。这样可以提供更好的性能并减少存储空间,但这也意味着您无法随意添加新属性。

Read more here: http://docs.python.org/reference/datamodel.html

在这里阅读更多内容:http://docs.python.org/reference/datamodel.html

#2


It's written in C

它是用C语言编写的

http://svn.python.org/view/python/trunk/Modules/datetimemodule.c?view=markup

It doesn't seem to implement setattr.

它似乎没有实现setattr。

#3


While the question has already been answered; if anyone is interested in a workaround, here's an example --

虽然问题已经得到解答;如果有人对解决方法感兴趣,这里有一个例子 -

mydate = datetime.date(2013, 3, 26)
mydate.special = 'Some special date annotation'  # doesn't work
...
class CustomDate(datetime.date):
    pass
mydate = datetime.date(2013, 3, 26)
mydate = CustomDate(mydate.year, mydate.month, mydate.day)
mydate.special = 'Some special date annotation'  # works

#1


My guess, is that the implementation of datetime uses __slots__ for better performance.

我的猜测是,datetime的实现使用__slots__以获得更好的性能。

When using __slots__, the interpreter reserves storage for just the attributes listed, nothing else. This gives better performance and uses less storage, but it also means you can't add new attributes at will.

使用__slots__时,解释器仅为列出的属性保留存储空间,而不保留其他内容。这样可以提供更好的性能并减少存储空间,但这也意味着您无法随意添加新属性。

Read more here: http://docs.python.org/reference/datamodel.html

在这里阅读更多内容:http://docs.python.org/reference/datamodel.html

#2


It's written in C

它是用C语言编写的

http://svn.python.org/view/python/trunk/Modules/datetimemodule.c?view=markup

It doesn't seem to implement setattr.

它似乎没有实现setattr。

#3


While the question has already been answered; if anyone is interested in a workaround, here's an example --

虽然问题已经得到解答;如果有人对解决方法感兴趣,这里有一个例子 -

mydate = datetime.date(2013, 3, 26)
mydate.special = 'Some special date annotation'  # doesn't work
...
class CustomDate(datetime.date):
    pass
mydate = datetime.date(2013, 3, 26)
mydate = CustomDate(mydate.year, mydate.month, mydate.day)
mydate.special = 'Some special date annotation'  # works