Example of variable exchange in Python: python variable exchange

Source: Internet
Author: User

Example of variable exchange in Python: python variable exchange

Python pursues conciseness, giving birth to many assignment rules for computation, and strives to be simplified, including two or more variable exchange values.
In normal language
Copy codeThe Code is as follows:
# Declare Variables
A = 50
B = 10
# Start switching. assign a value to a temporary variable before switching the variable.
Tmp =
A = B
B = tmp

In Python, It is very convenient to exchange values between two variables.
Copy codeThe Code is as follows:
# Declare Variables
A = 50
B = 10
# Start variable exchange
A, B = B,

You can even exchange multiple variables at the same time.
Copy codeThe Code is as follows:
A = 50
B = 10
C = 20
C, B, a = a, B, c

One operator can replace multiple variable values!


Python class instantiation

You do not understand the relationship between class variables and instance variables.
In the first example, append is an operation on class variables in init. Therefore, the Instance Object newmen1/2 does not have its own variable a, and class variables are accessed.
If you display a class variable,
Print Men. a, newmen1.a, and newmen2.a are the same. Point to the same variable.
In the second example, init generates the object's own variable a. Note that '= '! When the instance object calls init, each instance object has its own variable a, and you can no longer access the class variable through the instance object. In fact, this writing method is not good.
Then you can display the class variable again,
Print Men. a, newmen1.a, newmen2
> 0 1 2
If you want all objects to share class variables, you can write
Class Men:
A = 0
Def _ init _ (self, B ):
Men. a = B
Def sayHi (self ):
Print 'hello, my name is ', Men.
Instead of using self to represent the public variables of the Instance Object. It will only make you dizzy.

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.