Python learning notes-object-oriented basics and python learning notes
1. Classes and Instances
1.1 class definition
The class definition uses the class keyword, followed by the class name (usually starts with an upper case), followed(object)
Object is the class name inherited by this class. If it is not, it inherits the object class.
The class name + () is used for instantiation, and parameters need to be input.
class Student(object): pass
bart = Student()
The class property is defined in the _ init _ method (equivalent to the java constructor). The first parameter of this method is always self, parameters (except self) must be input during instantiation)
class Student(object): def __init__(self, name, score): self.name = name self.score = score
bart = Student('Bart Simpson', 59)
1.2 method parameters
The first parameter of the class method must be self and does not need to be passed in. Other parameters are the same as common functions. You can also use default parameters, variable parameters, and keyword parameters.
1.3 about instance variables
Because python is a dynamic language that allows you to add new variables to an instance, the number and name of two instance variables in a class are not necessarily the same.
1 >>> bart = Student('Bart Simpson', 59)2 >>> lisa = Student('Lisa Simpson', 87)3 >>> bart.age = 84 >>> bart.age5 86 >>> lisa.age7 Traceback (most recent call last):8 File "<stdin>", line 1, in <module>9 AttributeError: 'Student' object has no attribute 'age'
2. Access Restrictions
In Python, if the variable name of an instance is_ (Dual-baseline)
It becomes a private variable, which can only be accessed internally and cannot be accessed externally. To obtain or change the variable value externally, you can use the set and get methods. You also need to check the parameters in the set method. In addition, variables at the beginning and end of a horizontal bar are special variables.
1 class Student(object):2 3 def __init__(self, name, score):4 self.__name = name5 self.__score = score
3. Inheritance and Polymorphism
Similar to polymorphism and java, the inheritance of python follows the "open and closed principle": it is open to extensions and closed to modifications. However, python supports multi-inheritance. If the parent class has the same method, It will be searched from left to right during the call. The rest will be detailed later.
4. Get Object Information
4.1 type function: type (object), return type
4.2 isinstance function: isinstance (instance, class name), return a Boolean Value
4.3 dir function: dir (object), returns a list of all methods and attributes of the object.
4.4__xxx__
The attributes and methods of are of special purpose in Python. For example, the _ len _ method is equivalent to the following two methods: (I think one is function writing, one is class method writing)
1 >>> len('ABC')2 33 >>> 'ABC'.__len__()4 3
4.5getattr()
,setattr()
AndHasattr () function: You can directly operate on the status of an object. As follows:
1 >>> hasattr (obj, 'x') # Is there an attribute 'X? 2 True 3 >>> obj. x 4 9 5 >>> hasattr (obj, 'y') # Is there an attribute 'y? 6 False 7 >>> setattr (obj, 'y', 19) # Set an attribute 'y' 8 >>> hasattr (obj, 'y ') # Is there an attribute 'y? 9 True10 >>> getattr (obj, 'y') # Get the property 'y' 11 1912 >>>> obj. y # Get the property 'y' 13 19
You can input a default parameter. If the property does not exist, the default value is returned:
1 >>> getattr (obj, 'z', 404) # obtain the property 'Z'. If the property does not exist, the default value 4042 is returned.