The properties declared outside of Init in the Python class differ from the access and operation of object properties declared within Init

Source: Internet
Author: User

The difference between the properties declared outside of Init in the Python class and the access and operation of the object properties declared within init (interview questions)

1. Enter the following code in Ipython, what is the output?

In [1]:classclassout: ...: Out_mem='Out_mem'   ...:     PrintOut_mem ...:def __init__(self): ...: Inner_mem='Inner_mem'...: Self.inner_mem='Self.inner_mem'...: Self._inner_mem='Self._inner_mem'...: Self.out_mem='Self.out_mem'...: in [2]: out =classout () in [3]: out.out_memin [4]: out._inner_memin [5]: out.inner_memin [6]:classclassout: ...: Out_mem='Out_mem'   ...:     PrintOut_mem ...:def __init__(self): ...: Inner_mem='Inner_mem'...: Self.inner_mem='Self.inner_mem'...: Self._inner_mem='Self._inner_mem'...: self.out_mem1='self.out_mem1'...: out_memin [7]: Out =classout () in [8]: out.out_memin [9]: classout.out_memin [10]: classout.out_mem1in [11]: out._inner_memin [[]: Out.out_mem ='Out_mem modified by Object'In [13]: classout.out_memin [14]: out.out_memin [[]: Out.new_mem ='Clas'In [16]: out.new_memin [+]: o =classout () in [18]: o.out_memin [19]: classout.out_memin []: Classout.out_mem ='Out_mem modified by Class'In []: O.out_mem

Study:

1. Python interpreter handles interpreting class

2. Class initialization Definition Method

3. Class __init__ method with class object member definition, class instance member definition

4. Naming conventions When a class defines a member

5. The difference between class object and class instance

6. The difference between class object and class instance querying domain precedence when dealing with missing member access

7. Class definition __private_mem dollar not found externally? (renamed to _[CLASS_NAME]__PRIVATE_MEM)!!! See the code below!

In []: class Classout:

...: out_mem = ' Out_mem '

...: print Out_mem

...: def __init__ (self):

...: inner_mem = ' Inner_mem '

...: self.inner_mem = ' Self.inner_mem '

...: self._inner_mem = ' Self._inner_mem '

...: self.out_mem1 = ' self.out_mem1 '

...: self.__private_mem = ' Self.__private_mem '

...:

Out_mem

In [+]: no = Classout ()

In []: no.__private_mem

---------------------------------------------------------------------------

Attributeerror Traceback (most recent)

<ipython-input-50-36dd351a1b65> in <module>()

----> 1no. __private_mem

attributeerror: classout instance has no attribute ' __private_mem '

In [Wuyi]: no._classout__private_mem

out[]: ' Self.__private_mem '

8.1. Can't set properties directly to an object?
obj = Object ()obj. name = "Whatever" Traceback (most recent):  "<stdin>" 1 <module> Attributeerror ' object ' object has no attribute ' name '

But why this is possible:

class Object (object): Pass ...  obj=Object()obj. name = "Whatever"  Obj. name ' Whatever ' >>>

A: Now you give the object in the second code block a property __slots__ Try it:

 >>>  class  object   ( object  ):      ...  __slots__  =  {}   ...  >>>  obj  =  object   ()  >>>  obj   name  =   "whatever"   traceback (most recent call last):  File  "<stdin>" , line 1 , <module>  attributeerror :  ' object ' object has no attribute ' name '  

Will find that the same exception was thrown. This is true for built-in functions such as object , list , dict , and so on.

Classes that have the __slots__ attribute do not automatically assign __dict__ when instantiating an object, and obj.attr is obj.__dict__[' attr '. So it causes attributeerror .

For an instance of a class that has the __slots__ attribute, you can only set properties in __slots__ for obj :

>>>class Object(Object):...     __slots__ = {"a","B"}...>>>OBJ = Object()>>>OBJ.a = 1>>>OBJ.a1>>>OBJ.C = 1Traceback (most recent):File"<stdin>", line1, in<module>Attributeerror:' object ' object has no attribute ' C '

See Python-slots-doc for details

The properties declared outside of Init in the Python class differ from the access and operation of object properties declared within Init

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.