Maya Python single line class instance and class variable assignment? -


i want this,

things = [ node().path = x x in cmds.ls(sl=1,l=1)] 

but i'm getting invalid syntax error. i've had use instead,

things = [] i, thing in enumerate(cmds.ls(sl=1,l=1)):     things.append(node())     things[i].path = thing 

the first invalid code nice clean , short. second 1 tedious. how can code allows me assign class variable in same creation line without using initialization. i'm avoiding initialization because class going inherited many other classes across multiple files, , previous version uses initializations breaks horrible when imported many packages, causing unbound method errors.

not first example invalid syntax, attempting fundamentally unsound: do not mix list-comprehensions state-change (i.e. assigning object attribute). whatever node is, seems best way deal issue add parameter node.__init__ allows set path when instantiate node object. , things = [node(x) x in cmds.ls(sl=1, l=1)]

so, basic approach using single positional argument __init__:

class node(object):     def __init__(self, path):         self.path = path ...  things = [node(x) x in cmds.ls(sl=1, l=1)] 

more importantly, though, using for-loop perfectly pythonic. trying code one-liners fundamentally misguided. here how work have , make more pythonic:

things = [] path in cmds.ls(sl=1,l=1):     n = node()     n.path = path     things.append(n) 

the above pythonic is...


Comments