Python class three methods, function parameters, class and instance variables (i)

Source: Internet
Author: User
Tags instance method

1 Python's function pass:

First, all variables can be understood as a ' reference ' to an object in memory

a =  1  def  Func (a): a =  2  func (a) print  (a) # 1  a =  1  def  Fun (a): print  (" function ", id  (a)) # function 41322472  a =  2  print  ( "after assignment" , id  (a), id  (2 )) # After assignment 41322448 41322448  print  (, id  (a), id  (1 )) # function outside 41322472 41322472  Fun (a) print  (a) # 1  

As you can see, a = 2 after execution, the value stored in the reference, that is, the a memory address changes, is changed from the address of the original object to the 1 memory address of the 2 entity object. Let's look at another example:

= []def fun(a):    a.append(1)fun(a)print a  # [1]= []def fun(a):    print("函数里",id(a))  # 函数里 53629256    a.append(1)print("函数外",id(a))     # 函数外 53629256fun(a)print# [1]

Attention:

    1. A type belongs to an object, not a variable. There are two types of objects, "mutable" and "non-changing" (immutable) objects.
    2. strings, tuples, and numbers are immutable objects, while list, Dict, set, and so on are modifiable objects.
    3. When a reference is passed to a function, the function automatically copies a reference, and the reference in the function does not have a semi-gross relationship with the outside reference. So the first example is that the function refers to an immutable object number, and when the function returns, the outside reference does not feel half-hairy. And the second example is different, The reference in the function refers to the Mutable object list, which points to the memory address of the list, so the call does not change the list memory address.
2 python in Meta class

The classes in Python are also objects. A meta-class is a class that is used to create these classes (objects).

= MetaClass()    #元类创建= MyClass()     #类创建实例实际上MyClass就是通过type()来创建出MyClass类,它是type()类的一个实例;同时MyClass本身也是类,也可以创建出自己的实例,这里就是MyObject

A class is something that creates a class object, type is the inner Jianyuan class of Python, and of course you can create your own meta-class.

age =  35  Age.__class__ #输出: <type ' int ';  name =   Name.__class__ #输出: <type ' str ';  def  foo (): pass  foo.__class__ #输出: <type ' function ';  class  Bar (object ): pass  b =  Bar () B.__class__ what is the __class__ property for any one __class__? A.__class__.__class__ #输出: <type ' type ';  age.__class__.__class__# Output: <type ' type ';  foo.__class__.__class__ #输出: <type ' type ';  b.__class__._ _class__ #输出: <type ' type ';   
3 static methods (@staticmethod) and class methods (@classmethod)

Python actually has 3 methods, namely static methods (Staticmethod), class methods (Classmethod), and instance methods

defFoo (x):#常规方法    Print("Executing foo (%s)"%(x))classAObject):#实例方法    defFoo Self, x):#默认第一个参数为实例对象        Print("Executing foo (%s,%s)"%( Self, x))#类方法    @classmethod    defClass_foo (cls,x):#默认第一个参数为类对象        Print("Executing Class_foo (%s,%s)"%(cls,x))#静态方法    @staticmethod    #不需要绑定, call attention    defStatic_foo (x):Print("Executing Static_foo (%s)"%X) A=A ()

The self and the CLS inside the function arguments. This self and the CLS are bindings to instances or classes

    1. For general functions We can call foo(x) this function, which is most commonly used, and its work has nothing to do with anything (class, instance).
    2. For instance methods, we know that every time we define a method in a class, we need to bind to this instance, that is foo(self, x) , because the instance method call is inseparable from the instance, we need to pass the instance to the function, when it is called a.foo(x) (in fact foo(a, x) ).
    3. class method, except that it passes a class instead of an instance A.class_foo(x) . Note that the self and the CLS here can replace other parameters, but the Python convention is that both
    4. For static methods, like normal methods, there is no need to bind to who, the only difference is that the call needs to be used a.static_foo(x) or called A.static_foo(x) . Whether it's a class call or an instance calling a static method, it's all pointing to the same function object
# instance Method class Method Static Methods
A = A () A.foo (x) A.class_foo (x) A.static_foo (x)
A Not available A.class_foo (x) A.static_foo (x)
4 class variables and instance variables

class Variable: is a value that can be shared among all instances of a class (that is, they are not assigned individually to each instance).

instance variables: variables that are owned by each instance after instantiation.

classTest (Object): Num_of_instance= 0  #类变量    def __init__( Self, name):#name就是实例变量         Self. Name=Name Test.num_of_instance+= 1    if __name__ == ' __main__ ':Print(test.num_of_instance)# 0T1=Test (' Jack ')Print(test.num_of_instance)# 1T2=Test (' Lucy ')Print(T1.name, T1.num_of_instance)# jack 2    Print(T2.name, T2.num_of_instance)# lucy 2    classPerson:name="AAA"P1=Person () P2=Person () P1.name="BBB"PrintP1.name# BBBPrintP2.name# AAA object name. PropertiesPrintPerson.name# AAA class name. Properties

Here p1.name="bbb" is the instance called the class variable, similar to the problem of the function of the parameter, the p1.name first is a pointer to the class variable, but in the scope of the name="aaa" instance of the class variable to change the reference, it becomes an instance variable, Self.name no longer refers to the person's class variable name.

Python class three methods, function parameters, class and instance variables (i)

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.