[英]completion/intellisense on (*args ,**kwargs) functions
Is there a way to have completion/intellisense on (*args,**kwargs) functions?有没有办法在 (*args,**kwargs) 函数上完成/智能感知?
For instance:例如:
class GetVar(GetVarInterface):
@classmethod
def fromcustom(cls,locorvar,offset=0,varType="int", name=None,deref=False,member=None):
return GetVarCustom(locorvar,offset,varType, name,deref,member)
class GetVarCustom(GetVar):
def __init__(self,locorvar,offset=0,varType="int", name=None,deref=False,member=None):
I wanted to implement this without specifying every argument of the constructor (For example using *vars, **kwargs) but didn't want to lose completion/intellisense abilities.我想在不指定构造函数的每个参数(例如使用 *vars、**kwargs)的情况下实现它,但不想失去完成/智能感知能力。 Is there a way?有办法吗?
The disadvantage in the current implementation is that you would have to replicate the signature twice for every change...当前实现的缺点是您必须为每次更改复制签名两次......
The only option is to add a comment under the function to hint the arguments, otherwise you can't;唯一的办法就是在function下面加注释提示arguments,否则不行; if the ide is reading that a function has undefined arguments, it will show you that it's undefined.如果 ide 读到 function 有未定义的 arguments,它会告诉你它是未定义的。 A "solution" is to just use the common arguments and pass the rest as kwargs, or you can keep the original init.一个“解决方案”是只使用常见的 arguments 并将 rest 作为 kwargs 传递,或者您可以保留原始的 init.
class Single_Init:
def __init__(self, val_a, val_b, name=None):
self.val_a = val_a
self.val_b = val_b
self.name = name
class Single_Init_B(Single_Init):
# The previous contructor is calld
def get_result(self):
return self.val_a + self.val_b
class Split_Const:
def op_offset(self, offset):
self.offset = offset
def __init__(self, name, member=None, **kwargs):
""" You olso can hint in a func coment """
self.name = name
self.member = member
if 'offset' in kwargs:
self.offset = kwargs['offset']
else:
self.offset = None
if __name__ == '__main__':
single = Single_Init_B(2, 3)
print('Single:', single.get_result())
split = Split_Const('Name')
split.op_offset(0.5)
print('Split:', split.offset)
Got the solution outside this site..得到了这个网站之外的解决方案..
@functools.wraps(functools.partial(GetVarCustom.__init__,1))
def f(*args,**kwargs):
return GetVarCustom(*args,**kwargs)
Of course, it would have been easier in case of a standard function. However, you need to update the assigned attribute of wraps.当然,在标准 function 的情况下会更容易。但是,您需要更新包装的分配属性。 Otherwise it will change the function name.否则它将更改 function 名称。
@functools.wraps(GetVarCustom.value,assigned=['__doc__'])
def getvalue(*args,**kwargs):
return self_custom.value(*args,**kwargs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.