Classes in Python

Source: Internet
Author: User

class 1.1 includes: class properties, methods

OO the characteristics (Object oriented)

Encapsulation: Information hiding technology

Inheritance: inheritance is the mechanism by which subclasses automatically share data and methods between parent classes

class MyClass (list):     Pass>>> list1.append (4)>>> list1.append (3)>>> list1.append ( 7)>>> list1[4, 3, 7]>>> list1.sort ()>>> list1[ 3, 4, 7]

Polymorphism : different objects respond differently to the same method

OOA: Facing object analysis

OOP: Object-facing programming

OOD: Facing object Design

Object has a self argument that is equal to the C + + 's this pointer

By a class can generate countless objects, these objects are basically the same, when a method of an object is called, the object will itself (self) as the first parameter, when receiving this parameter, the Python class will know which object is calling the method


class Ball :     def SetName (self,name):             = name    def  Kick (self):            print(' I am%s, Why are you kicking me?  ',

Results after the execution:

>>> A.kick () I'm the ball A, why are you kicking me? >>> B.kick () I'm ball B, why are you kicking me? >>> C.kick () I'm ball C, why are you kicking me? 

1.2 The Magic method in Python:

Python's objects are inherently magical, and they are all about Python for the object, and they are a special way to add magic to your class, and if your object implements one of these methods, the method is called by Python in a special case. And all of this happens automatically.

These magic methods are defined by double underscores:

__init__ (Self): constructor method, the magic of this method is that when instantiating an object, then this method will be called (constructor in C + +) when the object is created, initialize the operation

1.2.1 public and private

By default, the properties and methods in a class are public and can be accessed by point operators

To implement features like private variables , Python uses a name mangling (name adaptation, name reorganization).

Defining a private variable in Python requires only two underscores in front of the variable name or function name, then the function or variable will be private.

class Person :     ' Little Turtle '

Can be accessed through methods within the class

Actually, the variables were manipulated.

Change the variable name to a

_ Class name __ variable name

The private variables in all of the above person classes can be accessed in the following ways:

>>> p = person ()>>> p._person__name' Little Turtle '

Classes in Python

Related Article

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: 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.