Python Dafa good-object-oriented

Source: Internet
Author: User

Python Dafa good-object-oriented

Python has been an object-oriented language since its inception, which is why it is easy to create a class and object in Python. In this section we will detail Python's object-oriented programming.

If you have not been exposed to object-oriented programming languages before, you may need to first understand some of the basic features of object-oriented languages and form a basic object-oriented concept in your mind, which will help you learn more about Python's object-oriented programming.

Let's start with a brief look at some of the basic features of object-oriented.

Introduction to Object-oriented technology
    • class: used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
    • Class variables: class variables are common throughout the instantiated object. Class variables are defined in the class and outside the body of the function. Class variables are not typically used as instance variables.
    • data member: A class variable or instance variable that handles the related data for a class and its instance objects.
    • method Overrides: if the method inherited from the parent class does not meet the requirements of the subclass, it can be overridden, which is called the override of the method, also known as the override of the method.
    • instance variable: A variable defined in a method that acts only on the class of the current instance.
    • inheritance: A derived class (derived class) that inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where an object of type dog is derived from the animal class, which is the analog "is a (is-a)" Relationship (example, dog is a animal).
    • instantiation: Creates an instance of a class, the concrete object of the class.
    • method: a function defined in a class.
    • object: An instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.
Create Class

Use the class statement to create a new class, followed by the name of the class and ending with a colon:

class ClassName:    ' class Help Information '   # class Document String   Class_suite  # class Body

The Help information for the class can be viewed through classname.__doc__.

Class_suite consists of class members, methods, and data properties.

Instance

The following is an example of a simple Python class:

classEmployee:'base class for all employees'Empcount=0def __init__(self, Name, salary): Self.name=name Self.salary=Salary Employee.empcount+ = 1defDisplaycount (self):Print "Total Employee%d"%Employee.empcountdefDisplayemployee (self):Print "Name:", Self.name,", Salary:", Self.salary
    • The Empcount variable is a class variable whose value is shared among all instances of the class. You can use Employee.empcount access in internal classes or outside classes.

    • The first method, the __init__ () method, is a special method called the constructor or initialization method of a class that is called when an instance of the class is created.

    • Self represents an instance of a class, and self is necessary when defining a method of a class, although you do not have to pass in the appropriate parameters when calling.

Self represents an instance of a class, not a class

There is only one special difference between a method of a class and a normal function--they must have an extra first parameter name , according to the Convention its name is self.

class Test:     def prt (self):         Print (self)         Print (self.) __class__  = Test () t.prt ()
Creating an Instance Object

The instantiation class is typically used in other programming languages with the keyword new, but in Python there is no such keyword, and the instantiation of the class is similar to how the function is called.

The following uses the name of the class Employee to instantiate and receive parameters through the __init__ method.

" to create the first object of an Employee class "  = Employee ("Zara", +)" Create second object of employee class " = Employee ("manni", 5000) 
Accessing properties

You can use the dot number . To access the properties of the object. Access the class variable using the name of the following class:

Emp1.displayemployee () emp2.displayemployee ( ) Print " Total Employee%d " % Employee.empcount
Python built-in class properties
    • __DICT__: Properties of the Class (contains a dictionary, consisting of the data properties of the Class)
    • __DOC__: Document string for Class
    • __NAME__: Class name
    • __MODULE__: The module where the class definition resides (the full name of the class is ' __main__.classname ', if the class is in an import module mymod, then classname.__module__ equals Mymod)

__BASES__: All parent classes of a class make up elements that contain a tuple of all the parent classes

Python Object Destruction (garbage collection)

Python uses the simple technique of reference counting to track and recycle garbage.

Inside Python is a record of how many references each object in use has.

An internal tracking variable, called a reference counter.

When the object is created, a reference count is created, and when the object is no longer needed, that is, the reference count of the object becomes 0 o'clock, and it is garbage collected. However, recycling is not "immediate", and the interpreter uses the garbage object to reclaim the memory space at the appropriate time.

The garbage collection mechanism not only targets objects with a reference count of 0, but can also handle circular references. Circular references refer to two of objects referencing each other, but no other variables refer to them. In this case, using only the reference count is not enough. The Python garbage collector is actually a reference counter and a cyclic garbage collector. As a supplement to the reference count, the garbage collector also pays attention to objects that are allocated a large amount (and those that are not destroyed by reference counting). In this case, the interpreter pauses to attempt to clean up all unreferenced loops.

classPoint :def __init__(Self, x=0, y=0): self.x=x self.y=ydef __del__(self): class_name= self.__class__.__name__      PrintClass_name,"destroyed"pt1=Point () pt2=PT1PT3=pt1PrintID (PT1), id (PT2), ID (PT3)#the ID of the print objectdelpt1delpt2delPt3

Not to be continued

Python Dafa good-object-oriented

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.