Python ()-Class namespace and object/instance namespace

Source: Internet
Author: User

Class namespace and Object/instance namespaces:

Creating a class creates a namespace for a class, space: A property of a storage class

Property:

    • Static properties: Directly define the variables that are associated with the & and class names below the class
    • Object properties: Variables associated with the & Class and objects in the class and self
    • Dynamic Properties: Methods (Functions)

The point of memory space:

Class FOO:A = 1b = [' Big ']def __init__ (self):p assf1 = Foo () print (f1.a) # 1f1.a = 2             # ' = ' break a pointer to a class, memory address is not a (from memory level) to a class , which points to the                immutable data type of the newly opened space object modification class, just assigns a new property to the object's own space print (f1.a) # 2print (f1.b) # [' Big ']f1.b.append (' small ' )     #没有断开, just point to the list append ()                                # Object modifies variable data type, global effect print (f1.b) # [' Big ', ' small ']

  

========

Class Foo:def Func (self): return 100f = Foo () print (F.func ()) F.func = 1         #修改成1, the object itself created a variable named Func = 1 Something print (f.func) 
    #f. Func without parentheses, is the object Call property print (Foo.func (f))         #如果还想用 100, you can use the class name. method, the self parameter must be passed in an object

  

================

Example: Number of times a statistic class was called

Class Foo:count = 0def __init__ (self): Foo.count + = 1f = foo () print (f.count) #1 foo.count F2 = foo () print (f2.count) #2 foo. COUNTF3 = foo () print (f3.count) #3 foo.countf = foo () F2 = foo () F3 = foo () print (F.count) # 3 Foo.countprint (F2.count) # 3 Fo O.countprint (F3.count) # 3 Foo.count

  

===============================

Outside the question:

Class Foo:count = 0def __init__ (self): Foo.count + 1def func (self): a = 3f = Foo () print (foo.func) #<function Foo.func at 0x0000000001e68950>print (F.func) Two memory address is not the same, (because bound bound to the object, there is a class pointer to the method of the class, not directly to get) #<bound method Foo.func of <__main__. Foo Object at 0x0000000001e67b38>>

  


================

Namespaces:

    • Static properties: namespaces that belong inside a class
    • Dynamic Properties: namespaces that belong inside a class
    • Object Properties: belonging to the object, associating self within the class, associating object names outside the class

View static properties: Class name. Property Object. Properties
Call a method in a class: Class name. Method name (Pass object), object. Method Name ()
Object can find class, class cannot find object, single line contact
Object lookup attribute, now own space to find, can't find again go to class space to find

Python ()-Class namespace and object/instance namespace

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.