Definition and use of the Python_class class

Source: Internet
Author: User

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

>>>

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.howmany ()

We have 0 persons here.

>>> B = person (' BB ')

(Initializing BB)

>>> B.howmany ()

We have 0 persons here.

>>> ^z

AA 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 usage

Class Canimal:

name = ' Unname ' # member variable

def __init__ (self,voice= ' Hello '): # overloaded constructors

Self.voice = Voice # Create a member variable and assign an initial value

def __del__ (self): # overloaded destructor

Pass # Empty operation

def Say (self):

Print Self.voice

t = canimal () # defines animal object T

T.say () # t talk

>> Hello # output

Dog = Canimal (' wow ') # defines animal object dog

Dog. Say () # Dog talking

>> 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 Classes

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 # NULL 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 = Cdog ()

Bobo. Setvoice (' My Name is bobo! ') # set Child.data to Hello

Bobo. Say ()

Bobo. Run ()

>> My Name is bobo!

>> Running

Definition and use of the Python_class class

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.