How is the parameter problem handled in the Python reflection mechanism?

Source: Internet
Author: User

Pythonin development, we often use the reflection mechanism, which is mainly the connection between the string and the module application, the core is to convert the string into a module that can be called, the module method of the variable. Today and we share the main reflection mechanism of the parameters of the processing related content, let's take a look, I hope to everyoneLearningPython has helped. First of all to introduce the nextthe following four methods: Hasattr (obj, name,/) Return whether the object has a attribute with the given name. This is do by calling GetAttr (obj, name) and catching Attributeerror. Hasattr ()is detectionobjdoes the inside containnameproperties (Functions, Methods...),hasattr ()returnTrueorFalse. GetAttr (object, name[, default]), value Get A named attribute from an object; GetAttr (x, ' Y ') is equivalent to x.y. When a default argument is given, it's returned when the attribute doesn ' t exist; Without it, an exception was raised in the case. GetAttr ()used to invokeObject.name, you can generally assign it to other variables. defaultis optional and is alsoObject.namenon-existent conditions, will return when not filledAttributeerror; If you set thedefaultvalue is returned, the user-setdefaultvalues. SetAttr (obj, name, value,/) Sets the named attribute on the given object to the specified value. SetAttr (x, ' Y ', V) is equivalent to "x.y = V" SetAttr ()set aObj.name = value, which means to set a new method (function) into the object. Delattr (obj, name,/) Deletes the named attribute from the given object. Delattr (x, ' Y ') is equivalent to ' del x.y ' Delattr ()DeleteObj.name, removing unwanted methods (attributes) from the object. set a very simple character-property code class role(object): n = "Zzh" Date = "2018" def__init__(self, name, weapon, clothes, live = 100): Self.name = Name Self.weapon = Weapon Self.clothes = Clothes Self.live = Live defBuy_weapon(Self, weapon_name): Print ("%s buy a%s"% (Self.name, weapon_name)) defGot_headshoot(self): Print ("%s got headshot!"%self.name) Self.live-= 50 defShow_inner(self): Print ("Live is%s"%self.live) def__del__(self): Print ("Game over!") Role_default = Role ("zzz", "AWM", "3-level") while True: Command = input (' Action: ') ifHasattr (Role_default, command): Command_act = GetAttr (Role_default,command) Command_act () Else: Print (' Command not exists! ')in the process of running,CommandInput In addition toBuy_weaponThere is no problem with the string outside. then look for it.Buy_weaponWhat is the difference between this function and other methods? turned out to beBuy_weaponis a function with two parameters' Weapon_name '. Let's take a look at the error message: Typeerror:buy_weapon () Missing 1 required positional argument: ' Weapon_name 'as we have guessed, there is a parameter missing. Navigate to the code and we can knowErrorout now: Command_act ()alsoCommand_actThe parameter was missing when the call was made' Weapon_name ', so we thought of a clumsy method ( the1.0): whileTrue: Command= Input (' Action: ') ifHasattr (Role_default,Command): Command_act = GetAttr (Role_default,Command) Try: Command_act () Except TypeError: Factor = input (' Needing a extra factor: ') Command_act (Factor)Else:Print(' Command not exists! ')All we need to do is simply add a little bitTryjudged, if judged out ofTypeErrorjust give him a parameter, all right? in the late expansion of the code found that we have to pass two parameters what to do?? The result is that we are missing one more parameter, if we want to pass three, four, or even 10,000 parameters in the future? we have to find a way to solve this problem once and for all! First of all, we can know that all parameters are dependent on the user input, that is, if there are 10,000 parameters, the user needs to enter 10,000 times. so we associate it with the cycle, by judgingTypeErroroccur or not to implement the loop input, as long as the result isTypeErrorWe continue to enter parameters until the function needs to be met. Source:Blog Park

How is the parameter problem handled in the Python reflection mechanism?

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.