In Java, both static variables (called class attributes in Python) and instance variables (called data attributes in Python) are defined immediately after the class definition (one with the static keyword, one without). In Python, only class attributes can be defined here; data attributes are defined in the
__init__ method.Class attributes can be used as class-level constants, but they are not really constants.
Python supports data attributes (called “instance variables” in Java and “member variables” in C++). Data attributes are pieces of data held by a specific instance of a class. To reference this attribute from code outside the class, you qualify it with the instance name,
instance.data, in the same way that you qualify a function with its module name. To reference a data attribute from within the class, you use self as the qualifier. By convention, all data attributes are initialized to reasonable values in the __init__ method. However, this is not required, since data attributes, like local variables, spring into existence when they are first assigned a value.
No comments:
Post a Comment