More abstract of the Python basic tutorial notes

Source: Internet
Author: User

    • Methods, functions, and attributes

The difference between a function and a method is that the self parameter, the method (bound method), will help top their first argument to the owning instance, so this parameter is not available. You can bind an attribute to a normal function so that there is no special self argument, in other words, the attribute can be bound to a method or bound to a function, the difference is that the method binds itself to an instance, such as

>>>classClass:defmethod (self):Print 'I have'>>>deffunction ():Print 'I do not'>>> instance =Clas ()>>> instance =Class ()>>>Instance.method () I have>>> Instance.method =function>>>Instance.method () I do not

The self parameter does not depend on how the method is called, the instance invocation method is used, and other variables that refer to the same method can be used arbitrarily, in other words, the self parameter is passed in, not just when the instance invokes the method, if the method is referenced by another variable, and the variable is used, The self parameter is also automatically passed in, as

class Bird:     ' Hello '    def Sing (self):         Print self.song>>> bird = Bird ()>>> bird.sing () Hello>>> Birdsong = bird.sing>>> birdsong () Hello
    • The skills of privatization

To make a method or feature private, simply precede its name with a double underline, and in the inner definition of the class, these names are "translated" into the form preceded by a single underscore and a class name, or with a single underline, with some practical effects, such as an imports statement that cannot be marked with an asterisk (from Module import *).

    • namespace of the class

The definition of a class is actually a code block, and all code in the class statement executes---class namespace in a special namespace, such as

>>>classC:Print 'Class C being defined ...'Class C being defined ...>>>classMembercounter:member=0definit (self): Membercounter.member+ = 1>>> M1 =Membercounter ()>>>M1.init ()>>>Membercounter.member1>>> m2 =Membercounter ()>>>M2.init ()>>>MEMBERCOUNTER.M>>>Membercounter.member2
    • Object-oriented model correlation

Sketch steps:

1. Write down a description of the problem (what does the program do?) ), underline all nouns, verbs, and adjectives

2. For all nouns, use as a possible class

3. For all verbs, as a possible method

4. For all adjectives, as a possible characteristic

5. Assigning all methods and features to classes

Refining Model:

1. Write down (or imagine) a series of use cases----that is the scenario when the program is applied, and try to include all the functions

2. Take a step-by-point look at each use case to ensure that the model includes everything you need. If there is something missing, add it in. If something is not correct, correct it. Continue until you are satisfied.

    • Other

You can specify a superclass by writing another class name in parentheses after the class statement, such as

Class Subclass (Superclass):

Def class

Issubclass--To see if a class is a subclass of another class

__bases__--Get the base classes of known classes

Isinstance--Check if an object is an instance of a class

__class__--Know which class an object belongs to

Hasattr--Check if the required method already exists

GetAttr--Get Object properties

SetAttr--Setting the properties of an object

More abstract of the Python basic tutorial notes

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.