Small knowledge points encountered in Python Learning

Source: Internet
Author: User

1. The pass statement does not do anything. It is generally used as a placeholder or creates a placeholder program. The pass statement does not perform any operations, for example:

 

While false:

Pass

 

Pass is usually used to create the simplest class:

 

Class myemptyclass:

Pass

 

In the software design phase, pass is often used as a todo to remind you to implement the corresponding implementation. For example:

 

Def initlog (* ARGs ):

Pass # Please implement this

 

Take the if statement as an example,

In C or C ++/Java:

If (true)

; // Do nothing

Else

{

// Do something

}

For python, write as follows:

If true:

Pass # do nothing

Else:

# Do something

 

2. _ init _ usage

_ Init _ is automatically called when the class is initialized to complete initialization of the object, for example:

Class person:

Def _ init _ (self, name ):

Self. Name = Name

Def sayhi (Self ):

Print 'hello, my name is ', self. Name

 

P = person ('swaroop ')

P. sayhi ()

 

 

Running result:

Hello, my name is swaroop

 

We can see from the results that although we do not display the call _ init _ method, the class person is still initialized.

 

 

3. _ name _ usage

 

If a. py file is run independently, _ name _ is set to _ main _, so you can use

If _ name __= = '_ main _': To make some judgments,

However, if the. py file is imported as a module, _ name _ Is Not _ main _ and will be set as the module name, for example:

>>> Import odbchelper

>>> Odbchelper. _ name __

'Oddbchelper'

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.