The thing is, these attribute names returned by “dir” are strings. How can I use this string to get or set the value of an attribute? We somehow need a way to translate between the world of strings and the world of attribute names. 我们发现上面例子中返回的属性都是字符串(string), 那我们如何获得这些属性呢? python提供了内置的方法getattr, 如下所示:
As with all assignments in Python, the new value can be any legitimate Python object. In this case, we’ve assigned f.x to be 5 and 100, both integers, but there’s no reason why we couldn’t assign a tuple, dictionary, file, or even a more complex object. From Python’s perspective, it really doesn’t matter.
>>> f.new_attrib = 'hello'
>>> f.favorite_number = 72
所以, 我们可以动态地创建和分配属性. 但是如果读取一个未定义的属性就会报错:
AttributeError: 'Foo' object has no attribute 'no_such_attribute'
This is one of those conventions that is really useful to follow: Yes, you can create and assign object attributes wherever you want. But it makes life so much easier for everyone if you assign all of an object’s attributes in init, even if it’s just to give it a default value, or even None. Just because you can create an attribute whenever you want doesn’t mean that you should do such a thing.
所以, 在python中其实没有所谓的”对象变量”或”类变量”的. 某些情况属性定义在类上, 而某些情况属性又定义在对象上.(当然, 类也是type对象, 在这里我们不讨论). 这也解释了为什么人们通常都会在类上定义属性, 因为这些属性对于它的对象来讲都是可见的.Yes, that is true, but it sometimes makes more sense than others to do so.
What you really want to avoid is creating an attribute on the instance that has the same name as an attribute on the class. 请看下面的例子:
We can actually see what attributes were actually set on the instance and on the class, using a list comprehension:
>>> [attr_name for attr_name in dir(f) if attr_name notin dir(Foo)]
>>> [attr_name for attr_name in dir(Foo) if attr_name notin dir(object)]
['__dict__', '__module__', '__weakref__', 'blah']
In the above, we first define “Foo”, with a method “blah”. That method definition, as we know, is stored in the “blah” attribute on Foo. We haven’t assigned any attributes to f, which means that the only attributes available to f are those in its class.