Pythonic方法避免了一堆if ... else语句?

时间:2011-04-12 19:37:03

标签: python

最近出现了好几次,我想比以前更好地处理它:我有一系列属性,我在对象和字典之间交叉引用。如果它们之间的值不同,我想将object.attribute设置为dictionary ['attribute']值。我还想跟踪发生了什么变化。

现在,我的第一个想法是只为每个属性使用if else语句,但在写完其中一些后,很明显我会一次又一次地重写相同的代码。必须有一种DRY方式来做到这一点,我只指定每次都在变化的部分,然后循环遍历所有属性。

在生产代码中,有15个不同的属性,但下面的示例仅使用2来简化。我对如何以巧妙的方式做到这一点有所了解,但我错过了实际设置object.attribute等于字典['attribute']值的最后一步。

# Simulated data setup - not under my control IRL
class someClass:
    def __init__(self, name, version):
        self.name = name
        self.version = version

objA = someClass('Test1','1.1')        
dictA = {'name':'Test1','revision':'1.2'}

# My code below        

# option 1 - a series of for loops
def updateAttributesSimple(obj, adict, msg):
    if obj.name == adict['name']:
        msg.append('Name is the same')
    else:
        msg.append('Name was updated from %s to %s' % (obj.name, adict['name']))
        obj.name = adict['name']

    if obj.version == adict['revision']:
        msg.append('Version is the same')
    else:
        msg.append('Version was updated from %s to %s' % (obj.version, adict['revision']))
        obj.version = adict['revision']        

# option 2 - trying to be clever about this
def updateAttributesClever(obj, adict, msg):
    attributeList = (('Name', obj.name, adict['name']),
                     ('Version', obj.version, adict['revision']))

    for valTuple in attributeList:
        if valTuple[1] == valTuple[2]:
            msg.append('%s is the same' % (valTuple[0]))
        else:
            msg.append('%s was updated from %s to %s' % (valTuple[0], valTuple[1], valTuple[2]))
            # code to set valTuple[1] = valTuple[2] goes here, but what is it?
            # valTuple[1] = valTuple[2] attempts to set the desired value to a string, rather than the attribute of obj itself            


msg = ['Updating Attributes simple way:']
updateAttributesSimple(objA, dictA, msg)
print '\n\t'.join(msg)

#reset data
objA = someClass('Test1','1.1')        
dictA = {'name':'Test1','revision':'1.2'}

msg = ['Updating Attributes clever way:']
updateAttributesClever(objB, dictB, msg)
print '\n\t'.join(msg)

这个想法就是这样,每当我需要添加另一个属性时,我就可以更新被检查的属性列表,并且已经编写了其余的代码。什么是Pythonic的方法来实现这个目标?

4 个答案:

答案 0 :(得分:5)

setattr()正在寻找:

attributeList = (('Name',    'name',    'name'),
                 ('Version', 'version', 'revision'))

for title, obj_attribute, dict_key in attributeList:
    obj_value = getattr(obj, obj_attribute)
    adict_value = adict[dict_key]

    if obj_value == adict_value:
        msg.append('%s is the same' % (obj_value))
    else:
        msg.append('%s was updated from %s to %s' % (title, obj_value, adict_value))

        setattr(obj, obj_attribute, adict_value)

答案 1 :(得分:2)

这适用于您:

class X(object):
    def __init__(self):
        self.a = 1
        self.b = 2

x = X()

d = dict()
d['a'] = 1
d['b'] = 3

def updateAttributes(obj,dic):
    def update(name):
        val = dic[name]
        if getattr(obj,name)==val:
            print name,"was equal"
        else:
            print "setting %s to %s" % (name,val)
            setattr(obj,name,val)

    for name in ['a','b']:
        update(name)

updateAttributes(x,d)
print x.a
print x.b

答案 2 :(得分:1)

您可能想要创建一个可以接受任意对象并将名称/值对的字典转换为更有意义的字符的函数。它不是严格意义上的“Python”策略,但在Python中相当容易,因为它支持闭包以及它如何处理引擎盖下的对象:

def checkUpdates( obj ):
    def updated( dictionaryPrevious, msg ):
        for name, value in dictionaryPrevious.items():
            if( obj.__dict__[name] == value ):
                msg.append('Name is the same')
            else:
                msg.append(name + 'has been changed!')
                obj.__dict__[name] = value
    return updated

我做了一个假设,字典中的名字总是对应于对象变量。如果它们不相同,则需要进行映射。

修改

() => []object => obj。多谢你们。有时你会从一种语言转到另外一种语言,这一切都变得混乱。

答案 3 :(得分:0)

有几个答案很接近,但为了处理dict中键的名称与相应对象的属性名称不匹配的事实,您需要一些方法来处理它。这可以通过添加另一个将字典中的键名称映射到对象属性名称的字典来轻松完成。

class someClass:
    def __init__(self, name, version):
        self.name = name
        self.version = version

objA = someClass('Test1','1.1')
dictA = {'name':'Test1','revision':'1.2'}
keymap = {'name':'name', 'revision':'version'}

def updateAttributesGeneric(obj, adict, key2attr, msg):
    for key, value in adict.iteritems():
        attrname = key2attr[key]
        if getattr(obj, attrname) == value:
            msg.append('%s is the same' % attrname)
        else:
            msg.append('%s has been changed' % attrname)
            setattr(obj, attrname, adict[key])

msg = ['Updating Attributes:']
updateAttributesGeneric(objA, dictA, keymap, msg)
print '\n\t'.join(msg)

# Updating Attributes:
#   name is the same
#   version has been changed