Python Learning Day 21st: Classes and objects, inheritance and derivation

Source: Internet
Author: User

Classes and objects

The essence of an object is a namespace, which holds its own unique properties, and the classes hold properties that are common to the object.

__INIT__ will automatically trigger when the class is called

There are two things that happen when you call a class:

1. Create an empty object stu1

2. Automatically trigger the __INIT__ function, passing the STU1 and the parameters in parentheses

Property Lookup

Look for the object's own namespace first, but not to the class.

Binding method

A function defined in a class is shared to an object and is used by a binding to an object.

Binding effect: Bind to who, who comes to call, who calls on who will be the first parameter

Supplementary: The function defined in the class, the class can actually be used, but most of it is for the object, so need to bring a parameter self

Class is type

Everything in Python is an object, and the class and type in Python3 is a concept, and the type is the class

#类型dict就是类dict

>>>List<class 'List'>#instantiated to 3 objects L1,l2,l3>>> l1=list ()>>> l2=list ()>>> l3=list ()#three objects have a binding method append, is the same function, but the memory address is different, the ID is the same>>>L1.append<built-inchMethod Append of List object at 0x10b482b48>>>>L2.append<built-inchMethod Append of List object at 0x10b482b88>>>>L3.append<built-inchMethod Append of List object at 0x10b482bc8>#The action binding method, L1.append (3), is to add 3 to L1 and will never add 3 to L2 or L3>>> L1.append (3)>>>l1[3]>>>l2[]>>>l3[]#Calling Class List.append (l3,111) is equivalent to L3.append (111)>>> List.append (l3,111)#L3.append (111)>>>l3[111]

Objects are highly integrated products that integrate data with methods that specialize in manipulating that data (binding methods)

Inheritance and derivation

A derivation is a newly defined attribute in a subclass that uses its own

1. What is inheritance

Inheritance is a new way of creating a class that is a subclass of a new class, inherited as a parent class

Characteristics: Subclasses inherit attributes from the parent class, and inheritance is the relationship between classes and classes

2. Why to Inherit

Reduce the redundancy of your code

3. How to use Inheritance

supports a class that inherits more than one parent class Foo (parent): Parent

Python3, if no class is inherited, inherits the object class by default and does not inherit by default in 2

New class: Any class that inherits object and subclasses of that class are new classes

The classes in Python3 are all modern classes

Classic class: A class that does not inherit object

Python Learning Day 21st: Classes and objects, inheritance and derivation

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.