Python Object-oriented

Source: Internet
Author: User

Python Object-oriented
At the beginning of Python design is an object-oriented language

Introduction to Object-oriented technology:
Classes class: Describes a collection of objects that have the same properties and methods
Class variables: Class variables are common in the entire instantiated object
Data members: Class variables or instance variables that handle data related to classes and instance objects
Method overrides: If a method inherited from a parent class does not meet the requirements of a subclass, it is overridden by a procedure called a method
Instance variable: A variable defined in a method that acts only on the class of the current instance
Inherited:
Instantiation: Creating 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

To create a class:
Use the class statement to create a class, followed by the name of the class and ending with a colon
Class ClassName:
"" "Class Help Information" ""
Class_suite

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

#!/usr/bin/python
#-*-Coding:utf-8-*-

Class Employee:
' base class for all employees '
Empcount = 0

def __init__ (self, Name, salary):
Self.name = Name
Self.salary = Salary
Employee.empcount + = 1

def displaycount (self):
Print "Total Employee%d"% Employee.empcount

def displayemployee (self):
Print "Name:", Self.name, ", Salary:", self.salary


The Empcount variable is a class variable
__init__ is a special method, the constructor or initialization method of a class.
Self represents an instance of the class, and self is required to define the class, representing the address of the current object, self. Class is pointing to classes
Self is not a keyword, you can change it to another



To create an instance object:
Instantiated classes typically use the New keyword in other programming languages, and instantiate a class like a function call in Python


Access properties:
You can use. To access the properties of an object

Properties can be accessed by accessing the function: (reflection)

GetAttr (obj, Name[,default]): Accesses the object's property hasattr (obj, name): Checks for the existence of a property setattr (obj, name): Add attribute delattr (obj, name): Delete Genus Of

Python built-in properties:

  __dict__ : The properties of a class (consisting of a field and a data property of a class) __doc__ : The document string for the class __name__ : Class name __module__: The module in which the class definition resides (the full name of the class is '__main__.classname', if the class is in an import module Mymod, So classname. __module__ equals Mymod) __bases__: An element that consists of all the parent classes of a class




Python Object Destruction (garbage collection)
Python uses the simple technology of reference technology to track and recycle trash
How many references to all objects in use are recorded inside Python
An internal tracking variable that becomes a reference counter
When the object is created, a reference count is created, and when the object is no longer needed, the object's reference count is 0 o'clock and is recycled
Recycling is not immediate, the memory of the object is collected by the timer at the appropriate time

Inheritance of the class:
One of the main benefits of object-oriented programming is the reuse of code, one of the ways to reuse it through the inheritance mechanism

class derived classes name (base class name)
....


1. If you need the constructor method of the parent class in the subclass, you need to display the constructor method of the calling parent class.
2. When calling a method of a base class, you need to prefix the class name of the base class with the Self argument variable
3.python always looks for a method of the corresponding type first, and if it cannot be found in the derived class, the lookup in the base class begins.

Issubclass (): Determines whether a class is a subclass of another class or descendant class, Issubclass (Sub, SUP)
Isinstance (obj, Class): Returns True if Obj is an instance object of class or is an instance object of class subclass


Method overrides: If a method inherited from a parent class does not meet the requirements of a subclass, it is overridden by a procedure called a method

Base Overloaded methods:


Operator Overloading:


Class Properties and methods:

Private properties of the class:
__private_attrs: Two underscores begin with, declaring that the property is private and cannot be used outside of the class or accessed directly. Self.__private_attrs when used in methods inside a class.

Methods of the class:
Inside a class, you can define a method for a class using the DEF keyword, unlike a generic function definition, which must contain the parameter self and be the first argument

Private methods of the class:
__private_method: Two underscores, declares that the method is a private method, and cannot be called outside of a class.
Python does not allow instantiated classes to access private data, but you can use Object._classname__attrname (object name. _ Class Name __ Private property name) to access the property

Single underline, double underline, and tail-to-bottom double underline description:

  __foo__ __init__  fromimport * __foo: A double underscore is a variable of private type, which is allowed to be accessed only by the class itself.

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