Python concept-reflection of the cultural heritage version: Anyway shot.

Source: Internet
Author: User

What is reflection

Light in the interface between the two materials to change the direction of transmission and return to the original phenomenon of the material, called Reflection!

Above, is the physical definition of reflection, with the concept of reflection in Python, completely without any relationship

The book belongs to the story: reflection

is to manipulate object-related properties in the form of a string.

Related functions of Reflection:

Hasattr (object,"name")# returns the bool type, judging if name exists in the namespace of object  # presence return True# does not exist return flase
obj = GetAttr (object,"name")# Gets the memory address named name in Object #  name can be a method, or it can be a property
SetAttr (Object,"name","Egon")#  Add a name name to the object and assign a value of Egon# in the object's __dict__ you can see {"Name": "Egon"}.
Delattr (object,"name")# remove memory address named name in Object

Why use reflection, what is the advantage of reflection?

Implement pluggable mechanisms

For example: Programmer A is responsible for writing the module, Programmer B is responsible for invoking, when programmer A's module is not finished, programmer B can still complete the code, and does not conflict with the work of programmer a

code example:

1 #edited by: Shanlong2 classFTPC:3     def __init__(self,x):4         Print("Come no baby", X)5     defGetFile (self):6         Print("There's files in there anyway .")7     defupfile (self):8         Print("you'll be uploading it. A file called Fuckegon.")
the unfinished module of programmer a
1 #edited by: Shanlong2 Importftpclient3 classFtpS:4     def __init__(self):5         Print("server is turned on")6     defGo (self):7FTPC = FTPCLIENT.FTPC ("Egon")8          whileTrue:9Userin = input (">>>:")Ten             if(Hasattr (Ftpc,userin)):#here is a way to determine if there is a method in the code that is not completed by programmer A, if it exists then execute, no prompt instruction error OneFTPP =getattr (Ftpc,userin) A FTPP () -             Else: -                 Print("instruction Error") the                 Continue -  -f =FtpS () -F.go ()
implementation of programmer B

Dynamic Import Module

When we import the module, we use the Import module name, and this module name is not a string, so the advantage is that the corresponding string can be converted into the corresponding module

code example:

1 #official recommended version: Egon Pit Cargo edition2 ImportImportlib#Import a improtlib module3Choice = input (">>>")#Get user input information (module name)4Importlib.import_module (Choice)#converts a user-entered string into the name of the module and imports the module5 6 #Short Gener preconceived version: officially not recommended, but if you want to write this, please write before writing:7 #< lamb >8Choice = input (">>>")#Get user input information (module name)9 __import__(Choice)#converts a user-entered string into the name of the module and imports the moduleTen #the > of </Lamb teaching

Python concept-reflection of the cultural heritage version: Anyway shot.

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.