A preliminary examination of the "Python" class

Source: Internet
Author: User

While testing has been defining and using some simple classes and methods, there are some ways to smattering the basics of the class, and it is not a good way to feel the problem. Take the time to see some basics today, make some notes, from "Python core programming" ~


First, class

1. Definition: A class is the definition of an object, and an instance is "real physical", which holds the specific information about the object defined in the class.

2. Create: The following shows how to create a class:

Class Mynewobjecttype (Bases):

' Define Mynewobjecttype class '

Class Suite

The keyword is class, followed by the class name, followed by the class body code that defines the class.

The new class must inherit at least one parent class , and the parameter bases can be one or more parent classes for inheritance.

The commonly used object is ' mother of all classes '. If your class does not inherit any other parent class, object will be the default parent class.

If there is no direct or indirect subclass of an object, then a classic class is defined:

Class Mynewobjecttype:

' Define Mynewobjecttype Classic class '

Class_suite

What do you mean? I understand that the parent class is not specified, or ' the subclass of the base class does not have a parent class '. The use of modern classes is highly recommended.

3. Operation: You can also do this by defining a class that does not have any properties of its own, using it to provide only a namespace for the data, and to instantiate and manipulate it.

Class MyData (object):

Pass

>>mathobj = MyData ()

>>mathobj.x =4

>>mathobj.y =5

>>mathobj.x + mathobj.y

As above, mathobj.x and Mathobj.y are instance properties, not class properties, note!



Second, the method

In Python, a method is defined in a class definition, but can only be called by an instance. In other words, the final way to invoke a method is this:

(1) Defining classes and methods

(2) Create an instance

(3) Invoking a method with an instance

Class MyData (object):

def PRINTFO (self):

print ' You're foo! '

The point is, the self parameter, which exists in all method declarations. This parameter represents the instance object itself, which is silently passed to the method by the interpreter when you invoke the method with the instance.


Class Addbookentry (object):

‘‘‘

Address Book Entry class

‘‘‘

def __init__ (self, NM, ph):

‘‘‘

Define the init Name&phone

‘‘‘

Self.name = NM

Self.phone = ph

print ' created instance for: ', self.name

def updatephone (SELF,NEWPH):

Self.phone = newph

print ' Update phone# for: ', self.name


John = Addbookentry (' John ', ' 123456 ')

John.updatephone (' 654321 ')


When the call is instantiated, it automatically calls __init__ (). self automatically passes the instance object into __init__ (). You can replace the self of the method with the instance name in your mind . After the object John is instantiated, its john.name is set. When you change an object jack is instantiated, its jack.name is set. In the same vein, the other.

If there are no default parameters, then the two parameters passed to __init__ () are required for instantiation .


Third, sub-category

Subclass by Inheritance is a way to create and customize new types. The new class will maintain the epitome of the existing class without altering the original class definition (meaning that changes to the new class will not affect the original class). For this new class, you can customize only the special features that belong to it.


Class Empladdbookentry (Addbookentry):

' Employee Address Book entry class '

def __init__ (Self,nm,ph,id,em):

Addbookentry.__init__ (self, NM, ph)

Self.empid = ID

Self.empem = em

def updateemail (SELF,NEWEM):

SELF.EMPEM = Newem

print ' Update e-mail address for: ', self.name


Jack = Empladdbookentry (' Jack ', ' 000000 ', ' e ', ' [email protected] ')

Jack.updateemail (' [email protected] ')


If required, each subclass is best defined by its own constructor, otherwise the constructor of the base class is called. However, if the subclass overrides the constructor of the base class, the constructor for the base class is not automatically called---so that the constructor of the base class must be explicitly written out before it is executed.


A preliminary examination of the "Python" class

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.