Definition, inheritance, and use of object instances in Python

Source: Internet
Author: User
The examples in this article describe the definition, inheritance, and methods of using objects in Python. Share to everyone for your reference. The specific analysis is as follows:

The concept of a class in Python programming can be likened to a description of a collection of types, such as "human", which can be regarded as a class and then defined by the human class as the object of every specific person-you, me, him, etc. Classes also have properties and functions, properties that are properties of the class itself, such as human beings with attributes such as name, height, and weight, while the specific values vary according to each person, and the function is the behavior that the class can achieve, such as the ability of humans to eat, walk, and sleep. The specific form is as follows:

Example: The concept of a class:

Class Human:
name = ' Unnamed ' # member variable
Def speaking (content): # member function
The print content # member variable assigns an initial value
someone = Human () # define a Human object someone
SB. 's name = "Passers-by"
SB. To speak (' Good people ') # talk to a passer-by
>>> Hello! # output

Example program one (Definition of Class):

>>> class pp:   ... Pass...>>> p = pp () >>> print p<__main__.pp instance at 0x00ca77b0>>>>

The type of the variable is printed. It tells us that we have an instance of the person class in the __main__ module.

Example program two (__init__ usage):

Description: The __init__ method runs immediately when the object of the class is established. This method is used to initialize an object.

>>> class Person: ...   def __init__ (self, name): ...       Self.name = Name   ... def sayhi (self): ...       print ' Hello, my name is ', self.name...>>> p = person (' Swaroop ') >>> p.sayhi () Hello, my name is SWAROOP&G T;>>

Example program three (__del__ method):

Description: The __del__ method is called when the program exits.

>>> class Person: ...   Population = 0   ... def __init__ (self, name): ...       Self.name = Name       ... print ' (Initializing%s) '% Self.name ...   def __del__ (self): ...       print '%s says bye. '% self.name ...       Person.population-= 1   ... def howmany (self): ...       if person.population = = 1: ...           print ' I am the only person here. ' ...       else:           ... print ' We have%d persons here. '% person.population...>>> A = person (' AA ') (Initializing aa) >>> A.howman Y () we have 0 persons here.>>> B = person (' BB ') (Initializing bb) >>> B.howmany () We have 0 persons here.> ;>> ^zaa says Bye.bb says bye.

In Python, the classes are defined and used in the form of the class class name [(parent class name)]:[member function and member variable], the class name is the name of the class, and the parent class name is optional, but after the parent class name is defined, the child class has the corresponding properties and methods of the parent class. When a class is defined as an object, the __init__ constructor is called first to initialize the properties of the object, and each property (member variable) of the class can be defined in the constructor, as long as the object pointer is added to the definition. When the object is destroyed, the __del__ destructor is called and when the member function of the class is defined, the default one variable (similar to the this pointer in C + +) represents the object itself defined by the class, the name of which can be defined by itself, and the following example uses the self variable to represent the class object variable.

Example: class definition and use:

Class Canimal:        name = ' Unname '       # member variable   def __init__ (self,voice= ' Hello '):  # overloaded constructor      Self.voice = Voice     # Create member variable and assign initial value   def __del__ (self):  # overloaded destructor      pass # null operation   def Say (self):       print Self.voice t = Canimal () # Definition Animal Object T T.say () # t talk >> Hello # output dog = Canimal (' wow ') # define Animal Object Dog Dog. Say () # Dog talk >> Wow # output

In Python programming, a class can inherit a parent class name (the parent class), a subclass that inherits all the methods and properties of the parent class, or the member functions and properties of the parent class, and it is important to note that if the subclass member function overloads the parent class (that is, the name is the same), the subclass member function is used

Example: Inheritance of a class

Class Canimal:     def __init__ (self,voice= ' Hello '):    # Voice initialization defaults to Hello        Self.voice = Voice     def Say (self) :       print Self.voice  def Run (self):       Pass # Empty Action statement (no Action) class Cdog (Canimal): # Inherit class Canimal   def setvoice ( Self,voice): # Subclass Add function     setvoice Self.voice = Voice     def Run (Self,voice): # Subclass overloaded function run            print ' Running ' bobo = CDo g () Bobo. Setvoice (' My Name is bobo! ') # set Child.data to Hello Bobo. Say () Bobo. Run () >> My Name is bobo! >> Running

Hopefully this article will help you with Python programming.

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