Python Notes-Class definitions

Source: Internet
Author: User
Tags class definition function definition

http://blog.csdn.net/wklken/article/details/6313265

First, class definition:

Class < category name:

< statements >

After a class is instantiated, its properties can be used, and in fact, after a class is created, its properties can be accessed through the class name

If you modify its properties directly using the class name, it will directly affect the object that has already been instantiated

Private properties of the class:

__private_attrs two underscores, declares that the property is private and cannot be used outside of the class or accessed directly

When used in methods inside a class Self.__private_attrs

Methods of the class

Inside the class, using the DEF keyword, you can define a method for a class that differs from a generic function definition, which must contain the parameter self, and is the first argument

Private class methods

__private_method two underscores, declares that the method is a private method and cannot be called outside of a class

Calling Slef.__private_methods inside a class

The proprietary methods of the class:

The __init__ constructor, which is called when the object is generated

__del__ destructors, using when releasing objects

__repr__ Printing, converting

__setitem__ Assignment by index

__getitem__ getting values by index

__len__ Get length

__CMP__ comparison operations

__call__ function call

__add__ Plus arithmetic

__SUB__ Reduction Operations

__mul__ Multiply operation

__DIV__ in addition to operations

__mod__ Calculating the remainder

__pow__ said Fang

Example:

[Python]View Plain Copy
  1. #类定义
  2. class people:
  3. #定义基本属性
  4. Name = ' '
  5. Age = 0
  6. #定义私有属性, private properties cannot be accessed directly outside the class
  7. __weight = 0
  8. #定义构造方法
  9. def __init__ (self,n,a,w):
  10. Self.name = n
  11. Self.age = A
  12. Self.__weight = W
  13. def speak (self):
  14. Print ("%s is speaking:i am%d years old"% (self.name,self.age))
  15. p = people (' Tom ', 10,30)
  16. P.speak ()

Second, the Inheritance class definition:

1. Single Inheritance

Class < category name > (parent class name)

< statements >

eg.

Class Childbook (book)

Age = 10

[Python]View Plain Copy
  1. #单继承示例
  2. Class student (people):
  3. Grade = ' '
  4. def __init__ (self,n,a,w,g):
  5. #调用父类的构函
  6. people.__init__ (SELF,N,A,W)
  7. Self.grade = g
  8. #覆写父类的方法
  9. def speak (self):
  10. Print ("%s is speaking:i am%d years old,and I am in grade%d"% (Self.name,self.age,self.grade))
  11. s = Student (' Ken ', 20,60,3)
  12. S.speak ()

2. Multiple Inheritance of classes

Class name (parent Class 1, parent Class 2,...., parent class N)

< statement 1>

Note the order of the parent class in parentheses, if the same method name is in the parent class and is not specified when the subclass is used, Python searches from left to right

If the method is not found in the subclass, look from left to right to see if the method is included in the parent class

[Python]View Plain Copy
  1. #另一个类, the preparation before multiple inheritance
  2. Class Speaker ():
  3. Topic = ' '
  4. Name = ' '
  5. def __init__ (self,n,t):
  6. Self.name = n
  7. Self.topic = t
  8. def speak (self):
  9. Print ("I am%s,i am a speaker! My topic is%s "% (Self.name,self.topic))
  10. #多重继承
  11. Class sample (Speaker,student):
  12. A = ' '
  13. def __init__ (self,n,a,w,g,t):
  14. student.__init__ (SELF,N,A,W,G)
  15. speaker.__init__ (self,n,t)
  16. Test = sample ("Tim", 25,80,4, "Python")
  17. Test.speak () #方法名同, the default is to call the method in parentheses before the parent class

Overriding of class methods-methods for subclasses covering out parent classes

Def method name is consistent with parent class

If you want to use the parent class name in the method to the parent class method. Method Name

If you put the class in the module

When used

Import A

L = A. Class ()

Results of implementation of the above three procedures:

Python Notes-Class definitions

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.