Python Object-oriented programming

Source: Internet
Author: User

1. Classes and instances

The class is the template that creates the instance, and the instance is a concrete object, each instance has its own data, and independent of each other;

A method is a function that binds to an instance, and unlike a normal function, a method of a class can directly access the data of an instance;

By invoking the method on the instance, you can implement the operation of the data inside the instance

Unlike static languages, Python allows you to bind any data to an instance variable, which means that for two instance variables, although they are different instances of the same class, the variable names you have may be different:

2. Access Restrictions

Class内部属性如果不想让外部直接访问,可以在变量前加__(两个下划线),如__name,而_name、__name__等都是可以直接访问的。

A private variable is not inaccessible, and the Python interpreter adds a class name in front of it so that it can be accessed in this way, such as Bob._student__name (Bob is an instance of class Student)

3. Inheritance and polymorphism:

Inheritance can take advantage of or override methods of the parent class.

When both the subclass and the parent class have the same run() method, we say that the child class run() overrides the parent class, and the subclass is run() always called when the code is running run() . In this way, we gain another benefit of inheritance: polymorphism .

This is the "duck type" of dynamic language, it does not require strict inheritance system, an object as long as "look like a duck, walk like a duck", it can be regarded as a duck.

Python's "File-like object" is a type of duck. For a real file object, it has a read() method that returns its contents. However, many objects, as long as there are read() methods, are considered "File-like object". Many functions receive the parameter "File-like object", you do not have to pass in the real file object, can pass any object that implements the read() method completely.

4. Get Object information:

Type (), Types,dir (), Isinstance (), GetAttr (), settattr (), hasattr ()

Always use Isinstance () to determine the type, and you can "clean sweep" The specified type and its subclasses.

getattr (), settattr (), hasattr () can determine if the corresponding variables, methods, and the corresponding variables and methods can be filled in the instance.

5. Class properties and Instance properties

An instance can add properties through Bob.name or SetAttr (), and a class can increment properties through in-class code, if the class and instance properties have the same name, the property of the priority invocation instance.

#练习题 to count the number of students, you can add a class attribute to the student class, and each time you create an instance, the property automatically increases:
Class Student (object):
Count = 0
def __init__ (self, name):
Self.name = Name
Student.count = student.count+1 # Note is a class student.count instead of an instance Selt.count

A = Student (' Bob ')
Print (Student.count)
A = Student (' Bob ')
Print (Student.count)
A = Student (' Bob ')
print (student.count)

Python Object-oriented programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.