Notes for python classes

Source: Internet
Author: User

1. Duplicate member variables with member functions

Data attributes with the same name overwrite the method attributes. To avoid possible name conflictsProgramWhich may be hard to find

Bug-it is best to avoid conflicts with some naming conventions. Optional conventions include uppercase letters of the method and lowercase letters of the data attribute name prefix (

It may be an underline), or the method uses a verb while the data attribute uses a noun.

Class test ():
Def _ init _ (Self ):
Self. A = 'OK'

Def A (Self ):
Print ('test ')

A = test ()
Print (A. a) # OK

2. Importance of naming conventions

Users should be careful when using data properties-The user may modify the data properties at will, thus undermining the Data Consistency originally maintained by the method.

. Note that you can add data attributes to the instance without affecting the method to avoid naming conflicts.

Validity-once again, naming conventions can save a lot of trouble.

It is best to set the read attribute of a member through the member function.

3. Private variable definition
The conventions of private variables start with less double underscores and end with at most single underscores.

Of course, you can use various methods to access private variables (for example, if the derived class and the base class have the same name, you can use the private variable of the base class.

Quantity .), Do not do this unless you know what you are doing.

#-*-Coding: UTF-8 -*-
Class test ():

Def _ FUNC (Self ):
Print ('test ')


A = test ()
A. _ FUNC () # variables that may not be accessed

4. Implement the Organization bodies in C language through classes
John = employee () # create an empty employee record

# Fill the fields of the record
John. Name = 'John Doe'
John. Dept = 'computer lab'
John. Salary = 1000

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.