关于python反射机制中的参数问题处理

python的反射机制十分的常用,主要是字符串与模块应用之间的连接方法。核心是将字符串转换成可以调用模块、模块方法的变量。

主要包括了以下四个方法:

hasattr(obj, name, /)

Return whether the object has an attribute with the given name.

This is done by calling getattr(obj, name) and catching AttributeError.

hasattr()是检测obj里面是否包含了name属性(函数、方法……),hasattr()返回True或者False。

 

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 is returned when the attribute doesn't exist; without it, an exception is raised in that case.

getattr()用来调用object.name,一般可将其赋值给其他变量。default是可选项,同样是object.name不存在的条件下,在不填的时候将返回AttributeError;如果设置了default值,则返回用户设置的default值。

 

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()设置一个obj.name = value,也就是说设置一个新的方法(函数)到对象里面。

 

delattr(obj, name, /)

Deletes the named attribute from the given object.

delattr(x, 'y') is equivalent to ``del x.y''

delattr()删除obj.name,把对象里不需要的方法(属性)去除。

 

设置一个十分简单的人物属性代码

class role(object):
    def __init__(self, name, weapon, clothes, life = 100):
        self.name = name
        self.weapon = weapon
        self.clothes = clothes
        self.life = life

    def buy_weapon(self, weapon_name):
        print("%s buy a %s" %(self.name, weapon_name))

    def got_headshoot(self):
        print("%s got headshot!" %self.name)
        self.live -= 50

    def show_life(self):
        print("Life is %s" %self.life)

    def __del__(self):
        print("Game Over!")

role_default = role("zzz", "AWM", "3-level")

while True:
    command = input('action:')
    if hasattr(role_default, command):
        command_act = getattr(role_default,command)
        command_act()
    else:
        print('Command not exists!')

 

在运行的过程中,command输入除了buy_weapon以外的字符串都是没有问题的。

那就来找找看buy_weapon这个函数跟其他方法有什么不同呢?

原来是buy_weapon是一个带有两个参数的函数,包括的额外的'weapon_name'。那我们来看一下报错信息:

TypeError: buy_weapon() missing 1 required positional argument: 'weapon_name'

跟我们猜测的一样,果然是少了一个参数。

 

定位到代码上,我们可以知道Error出现在:

command_act()

也就是command_act调用的时候缺少了参数'weapon_name',于是我们想到了一个笨拙的方法(传参1.0):

while True:
    command = input('action:')
    if hasattr(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!')

我们只需要简单的添加一下try判断,如果判断出来TypeError就给他传个参数就好了呗!

在代码后期的拓展中发现,我们要传两个参数怎么办??结果是我们又缺少了一个参数,那如果以后要传三个、四个、甚至一万个参数呢?就只能如同以下代码一样不停地增加try-except个数,这样子将会大大地增加工作量。

try:
    command_act()
    except TypeError:
        V1 = input('Missing Variable:')
        try:
            command_act(V1)
        except TypeError:
            V2 = input('Missing Variable2:')
            command_act(V1,V2)

 

于是我们想到设置一个循环去判断到底应该传多少个参数进去以及把用户输入当作参数传到函数:

要用到一个比较冷门的代码用了统计函数所需参数的个数:parameter_count = command_act.__code__.co_argcount

我们在知道所需参数个数后定义一个空列表用来“装”这些参数的值,接下来我们写一个循环来让用户循环输入所需要的参数。

接下来我们可以学到一个额外的知识,用来批量生产变量名:

names = locals()
names['v%s'%i] = input('Input Variable:')

locals()是python用来管理变量的字典,py文件中的变量名被储存到locals()字典里作为key,值为value。

最后用到一个很重要的知识,把列表转换成为参数传入函数: function(*list),总体代码如下:

class role(object):
    def __init__(self, name, weapon, clothes, life = 100):
        self.name = name
        self.weapon = weapon
        self.clothes = clothes
        self.life = life

    def buy_weapon(self, weapon_name):
        print("%s buy a %s" %(self.name, weapon_name))

    def got_headshoot(self):
        print("%s got headshot!" %self.name)
        self.life -= 50

    def fight(self,weapon,place):
        print("%s use %s to shoot %s" %(self.name,weapon, place))


    def show_life(self):
        print("Life is %s" %self.life)

    def __del__(self):
        print("Game Over!")

role_default = role("zzz", "AWM", "3-level")





while True:
    command = input('action:')
    if hasattr(role_default, command):
        command_act = getattr(role_default,command)
        parameter_count = command_act.__code__.co_argcount
     if parameter_count == 1: # self is also a parameter no need to input command_act() else: v_list = [] for i in range(1,parameter_count): parameter = input('Input Variable:') v_list.append(parameter) command_act(*v_list) else: print('Command not exists!')

 

该代码就可以实现无限参数的传入,这样我们在不改变类里面的代码逻辑和代码的条件下,完成了无论类里面的函数有多少参数都可以通过用户输入来解决。

 

posted @ 2018-04-07 12:05  弓心  阅读(1386)  评论(0编辑  收藏  举报