函数参数名称的别名(重复命名) [英] Aliasing in the names of function arguments (double naming)

查看:87
本文介绍了函数参数名称的别名(重复命名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,函数是否可以为其参数使用双名?我的意思是变量名的缩写和完整形式.

Are the functions able to have double names for their arguments in Python? I mean a short and full form of the variable name.

我会变得更加清楚.每个熟悉Autodesk Maya的人都知道进行约束的功能.它具有一些标志,您可以使用其名称的短号或长号:

I'll try to be more clear. Everybody who is familiar with Autodesk Maya knows a function to make constraints. It has some flags and you can use the short or the long form of it's name:

maintainOffset(mo),weight(w),layer(l)等.

maintainOffset(mo), weight(w), layer(l) and so on..

因此您可以使用不同名称的参数来调用此函数,但它会为您提供相同的结果:

So you can call this function with different names of it's arguments but it gives you the same result:

cmds.parentConstraint(driverObj, drivenObj, maintainOffset=True, weight=1.0,..)
cmds.parentConstraint(driverObj, drivenObj, maintainOffset=True, w=1.0,..)
cmds.parentConstraint(driverObj, drivenObj, mo=True, weight=1.0,..)
cmds.parentConstraint(driverObj, drivenObj, mo=True, w=True,..)

如何在Python 2.7.x中实现这种行为?我正在积极使用文档,但仍然找不到答案.

How to implement this type of behaviour in Python 2.7.x? I'm using the documentation actively but still can't find the answer.

此外,我还为各种约束定义了4个函数:

# Parent Constraint
def doParentConstraint(lst, mo = True, w = 1.0, sr = 'None', st = 'None', l = 'None'):
    for pair in lst:
        cmds.parentConstraint(pair[0], pair[1], maintainOffset = mo, weight = w,
                                skipRotate = sr, skipTranslate = st, layer = l)
# Orient Constraint
def doOrientConstraint(lst, mo = False, w = 1.0, o = (0.0, 0.0, 0.0), sk = 'None', l = 'None'):
    for pair in lst:
        cmds.orientConstraint(pair[0], pair[1], maintainOffset = mo, weight = w, 
                                offset = o, skip = sk, layer = l)
# Point Constraint
def doPointConstraint(lst, mo = False, w = 1.0, o = (0.0, 0.0, 0.0), sk = 'None', l = 'None'):
    for pair in lst:
        cmds.orientConstraint(pair[0], pair[1], maintainOffset = mo, weight = w, 
                                offset = o, skip = sk, layer = l)
# Scale Constraint
def doScaleConstraint(lst, mo = False, w = 1.0, o = (0.0, 0.0, 0.0), sk = 'None', l = 'None'):
    for pair in lst:
        cmds.orientConstraint(pair[0], pair[1], maintainOffset = mo, weight = w, 
                                offset = o, skip = sk, layer = l)

连接列表:

cLst = produceConnectionList(tNodesA, tNodesB)

以及所有这些功能的包装器功能:

def batchConnect(type = "parentConstraint", ???):
    cLst = produceConnectionList(tNodesA, tNodesB)
    if type is "parentConstraint": doParentConstraint(cLst, ???)
    if type is "orientConstraint": doOrientConstraint(cLst, ???)
    if type is "pointConstraint": doPointConstraint(cLst, ???)
    if type is "scaleConstraint": doScaleConstraint(cLst, ???)

但是我不知道如何在这些函数和包装器之间传递值,因为尽管它们具有相同数量的参数,但是参数的名称和那些类型略有不同.

But I don't know how to pass values between these functions and the wrapper because although they have the same number of arguments but the names of arguments and those types a little bit differ.

我还想在调用包装器时使用标志名的短格式和完整格式:

Also I want to have an opportunity to use short and the full form of the flag names when I'm calling the wrapper:

batchConnect("pointConstraint", mo=False, offset=(0,0,0), weight=1)
batchConnect("pointConstraint", maintainOffset=False, o=(0,0,0), w=1)

必须做同样的事情.

batchConnect("pointConstraint")

不带参数的必须使用默认值调用 doPointConstraint().

Without arguments must call doPointConstraint() with default values.

batchConnect()

完全不指定类型和参数的情况下,默认情况下必须使用这些默认值调用 doParentConstraint()

Without specifying the type and arguments at all must call by default doParentConstraint() with these default values

推荐答案

def myExample(**kwargs):
    if kwargs.get("width") and kwargs.get("w"):
        cmds.error("same flag used two times")
    else:
        myWidth = kwargs.get("width") or kwargs.get("w") or "nothing"
    print(myWidth)

myExample(width=200)

我曾经在某些脚本中这样做

I'm used to do this in some of my scripts

编辑---

如果必须多次,创建自定义类可能很容易:

If you have to this multiple times, it may be easy to create a custom class :

class mimicKwargs:
    def __init__(self, labelLong, labelShort, defautValue,kwargDic):
        if kwargDic.get(labelLong) and kwargDic.get(labelShort):
            cmds.error("same flag used two times")
        else:
            self.userInput = kwargDic.get(labelLong) or kwargDic.get(labelShort) or defautValue
    def output(self):
        return self.userInput

def myExample(**kwargs):

    myWidth = mimicKwargs("width", "w", 150, kwargs).output()

    print(myWidth)

myExample(width=200)#
myExample()#print 150

这篇关于函数参数名称的别名(重复命名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆