The instance explains the private attributes in Python, And the instance explains the python private attributes.

Source: Internet
Author: User

The instance explains the private attributes in Python, And the instance explains the python private attributes.

In Python, you can add a Double underline before the property variable name to define the property as a private property. For example:
Copy codeThe Code is as follows:
#! Encoding = UTF-8
 
Class:
Def _ init _ (self ):

# Defining private attributes
Self. _ name = "wangwu"

# General attribute Definition
Self. age = 19

A = ()
 
# Normal output
Print a. age
 
# The system prompts that the property cannot be found.
Print a. _ name

Execution output:
Copy codeThe Code is as follows:
Traceback (most recent call last ):
File "C: \ Users \ lee \ Documents \ Aptana Studio 3 Workspace \ testa \ a. py", line 19, in <module>
Print a. _ name
AttributeError: A instance has no attribute '_ name'

When you access the private attribute _ name, the system prompts that the property member cannot be found, rather than the permission. Therefore, when you write this code, no error is reported:
Copy codeThe Code is as follows:
#! Encoding = UTF-8
 
Class:
Def _ init _ (self ):

# Defining private attributes
Self. _ name = "wangwu"

# General attribute Definition
Self. age = 19

 
A = ()
 
A. _ name = "lisi"
Print a. _ name

Execution result:
1
Lisi
In Python, private variables cannot be accessed even if they are inherited. For example:
Copy codeThe Code is as follows:
#! Encoding = UTF-8
 
Class:
Def _ init _ (self ):

# Defining private attributes
Self. _ name = "wangwu"

# General attribute Definition
Self. age = 19

 
Class B ():
Def sayName (self ):
Print self. _ name

 
B = B ()
B. sayName ()

Execution result:
Copy codeThe Code is as follows:
Traceback (most recent call last ):
File "C: \ Users \ lee \ Documents \ Aptana Studio 3 Workspace \ testa \ a. py", line 19, in <module>
B. sayName ()
File "C: \ Users \ lee \ Documents \ Aptana Studio 3 Workspace \ testa \ a. py", line 15, in sayName
Print self. _ name
AttributeError: B instance has no attribute '_ B _ name'

Or the private attributes of the parent class access subclass are not allowed, for example:
Copy codeThe Code is as follows:
#! Encoding = UTF-8
 
Class:
Def say (self ):
Print self. name
Print self. _ age

 
Class B ():
Def _ init _ (self ):
Self. name = "wangwu"
Self. _ age = 20
 
B = B ()
B. say ()

Execution result:
Copy codeThe Code is as follows:
Wangwu
Traceback (most recent call last ):
File "C: \ Users \ lee \ Documents \ Aptana Studio 3 Workspace \ testa \ a. py", line 15, in <module>
B. say ()
File "C: \ Users \ lee \ Documents \ Aptana Studio 3 Workspace \ testa \ a. py", line 6, in say
Print self. _ age
AttributeError: B instance has no attribute '_ A _ age'


How does Python provide a private Method for class elements (attributes and methods?

Python does not support private members. All Python class members are public and can be called or accessed from outside the class.

The method of adding one or two underscores "_ func" or "_ member" before the member name is just a convention, prompting the user that this member function or variable is only used for internal calls, it is not designed for users. Even so, you can call the API without any restrictions.

Python class instantiation

1. In a python class, a private variable or function starts with _ (two underscores) but does not end with _. Private functions and variables cannot be called out of class.
Class test:
Def _ init _ (self, num ):
Self. _ num = num
PrivateTest = test (100)
PrivateTest. _ num # An error is returned.
Of course, there is also a way to adjust it, but it is not recommended to do that.
2. The variable s in the first class se () is a class variable, which can be accessed by the class itself, such as se. s, can also be accessed by various objects, and the value is unique because it exists in the class, a bit like static in C ++.
However, if an object also creates a variable named s that overwrites the class variable, the self. s is the property of the object and will not be adjusted to the class variable.
You can run it.
#-*-Coding: cp936 -*-
Class:
Name = []
Def _ init _ (self, name ):
Self. name. append (name)

Def nameMyself (self, name ):
Self. name = [name]
Print 'My name is ', self. name,' and class A1name is: ', A. name

Def test (self ):
Print "my name is", self. name
Obj = A ("num0 ")
Obj1 = A ("num1 ")
Print "obj1 'name", obj1.name # object metadata class variable name
Print "class A 'name", A. name # class variable name
Obj1.test () # The name Of The accessed class variable.
Obj1.nameMyself ('aid ') # Give yourself a name that overwrites the name of the class variable
Obj1.test () # For obj1, you can only access your own name.
Print "class a' name", A. name # The class variable still exists.

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.