Can't set attributes on instance of “object” class

后端 未结 7 1219
囚心锁ツ
囚心锁ツ 2020-11-22 10:10

So, I was playing around with Python while answering this question, and I discovered that this is not valid:

o = object()
o.attr = \'hello\'
<
7条回答
  •  花落未央
    2020-11-22 10:39

    It's because object is a "type", not a class. In general, all classes that are defined in C extensions (like all the built in datatypes, and stuff like numpy arrays) do not allow addition of arbitrary attributes.

提交回复
热议问题