Files
coding-mentor-agent/kb/python-course-kb-practical-python/wiki/exercises/5-2-modification-of-instance-data.md
T

1.5 KiB

id, source_exercise_id, title, section, source_path, source_repo, source_commit, student_visible_solution, has_private_solution, skip
id source_exercise_id title section source_path source_repo source_commit student_visible_solution has_private_solution skip
practical-python-5.2 5.2 Modification of Instance Data 5.1 Dictionaries Revisited 05_Object_model/01_Dicts_revisited.md https://github.com/dabeaz-course/practical-python 93dca856b41c61a0a0f85ae334116e4c125629ea false false false

Exercise 5.2: Modification of Instance Data

Source: Practical Python Programming, 05_Object_model/01_Dicts_revisited.md.

Exercise 5.2: Modification of Instance Data

Try setting a new attribute on one of the above instances:

>>> goog.date = '6/11/2007'
>>> goog.__dict__
... look at output ...
>>> ibm.__dict__
... look at output ...
>>>

In the above output, you'll notice that the goog instance has a attribute date whereas the ibm instance does not. It is important to note that Python really doesn't place any restrictions on attributes. For example, the attributes of an instance are not limited to those set up in the __init__() method.

Instead of setting an attribute, try placing a new value directly into the __dict__ object:

>>> goog.__dict__['time'] = '9:45am'
>>> goog.time
'9:45am'
>>>

Here, you really notice the fact that an instance is just a layer on top of a dictionary. Note: it should be emphasized that direct manipulation of the dictionary is uncommon--you should always write your code to use the (.) syntax.

关联来源