python--object-oriented and module complement, reflection

Source: Internet
Author: User

In Python, a private property can be set through __ (two underscores), and in fact an underscore also represents a private property, but it can still be accessed

There is no real privatisation support for private properties, method--python, but you can use underscores to get pseudo-private, so try to avoid defining variables that start with the underscore

A member variable that starts with a "single underscore" is called a protection variable, meaning that only class objects (that is, class instances) and subclass objects can access these variables themselves, accessed through the interface provided by the class, and cannot be imported using ' from module import * ', which is similar to PHP protected protected, can be accessed directly by instantiating objects and subclasses

class People (object):

_test1 = "An underlined private property"

__test = ' Two underlined private properties '

def __init__ (self,name):

self. Name = Name

def Run (Self):

Print ('%s is running ' %self. Name)

p = people ("Jack")

P.run ()

Print (P._TEST1)

A "double underline" begins with a private member, meaning that only the class object can access it, and the child object cannot access the data.

class People (object):

_test1 = "An underlined private property"

__test = ' Two underlined private properties '

def __init__ (self,name):

self. Name = Name

def Run (Self):

Print ('%s is running ' %self. Name)

p = people ("Jack")

P.run ()

Print (P._TEST1)

Print (p.__test)

Print (people.__test)


The class name is also not directly accessible:

Actually, it's just an appearance. View by __dict__ so attribute

Print (people.__dict__)


In fact, Python re-defines the private variable of the double underscore, so it can be accessed directly through the name of the class name __.

Print (p._people__test)


Direct access to

Access to private methods:


Normal is accessed through methods provided internally by the class

Private properties that cannot be accessed directly through the object, but can be accessed by means of a method

Private method that cannot be accessed directly by the object

Private properties, methods, are not inherited by the quilt class, and cannot be accessed

In general, private properties, methods are not disclosed, often used to do internal things, play a safe role

You can indirectly access the parent class's private methods, properties, by calling the shared method of the inherited parent class

For encapsulation, the class itself is a wrapper, the definition of a private property method is also a encapsulation, there is also an internal implementation of the logic, external access, only listen to provide an interface for outside use.

Reflection:

Python-oriented reflection in objects: manipulating Object-related properties in the form of strings. All things in Python are objects (you can use reflection)

Four functions that can be self-reflective

The following methods apply to classes and objects (everything is an object, and the class itself is an object)


Perform:

M ()


Setting properties

#设置属性

SetAttr (P,' School ',"Qinghua") #等于p. school= ' Qinghua '

Print (P.school)


#修改也可以

SetAttr (P,' name ',"Lucy")

Print (P.name)


Delete:

SetAttr (P,' name ',"Lucy")

Print (P.name)

delattr (P,"School")

delattr (P,"Schools") #删除不存在的会报错

#print (P.school)


Reflection can be defined in advance interface, the interface is only after the completion of the actual execution, this implementation of Plug and Play, which is actually a ' late binding ', meaning that the main logic can be written in advance (only to define the interface), and then later to implement the interface function,

Import other modules and use reflection to find out if a method exists for the module

A file. im2.py

class A ():

# ' A is defined first, but no specific function has yet been implemented '

def __init__ (self,addr):

Print (' defined interface not implemented ')

self. addr=addr

b file. demo.py

from im2 import A

A = A ("China")

if hasattr (A,' get '):

func_get=getattr(a,' get ')

Func_get ()

Else:

Print ('----> Not present this method ')

Print (' Handle other logic ')


Print ("im2 file")

def Test ():

Print ("Test")

def test1 ():

Print ("Test1")

Module_test = __import__(' demo.im2 ') #获取文件名

Print (module_test)


You can see that the imported files are executed after import

Module_test = __import__(' im2 ')

Print (module_test)

Module_test.test () #直接调用, if it is a folder to write the name of the package such as im2.py in the demo folder, Module_test.im2.test ()


When the module is imported, if it is used, the From module import * Then the private method cannot be imported

Will error

If you change to a direct call to run, this means that Python does not have an absolute private mechanism


Double underline is the same

python--object-oriented and module complement, reflection

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.