Make an Argument Optional in Python -
this question has answer here:
i'm making script gui box in cad program, , user selects 7 different surfaces in viewport. pass values onto function when user hits "ok"
the function passed looks this
def meshingtools(od_idsurf, trgsurf, pipebodysurf, sealsurf, threadsurf, bodysurf, cplgendsurf): the problem is: if user not need select 1 of surfaces, error saying, meshingtools() takes 7 non-keyword arguments (2 given)
how can around issue?
update:
i tried keyword arguments , not quite getting need.
def meshingtools(**kwargs): print kwargs
when select 1 surface, following out
{'pipebodysurf': (mdb.models['fullcal4'].rootassembly.instances['pinnew-1'].edges[151], mdb.models['fullcal4'].rootassembly.instances['pinnew-1'].edges[153])}
if try print pipebodysurf , says global name not defined.
any ideas?
final update (solved)
now see **kwargs creates dictionary, instead of using parameter name in rest of code, have use kwargs['parameter'] , use values
you can use arbitrary argument passing * operation :
def meshingtools(*args): in args: #do stuff functions can use special argument preceded 1 or 2 * character collect arbitrary number of arguments. (* positional arguments , ** keyword arguments)
Comments
Post a Comment