attributes - Python - Re-Implementing __setattr__ with super -
i know 1 has been covered before, , perhaps isn't pythonic way of constructing class, have lot of different maya node classes lot @properties retrieving/setting node data, , want see if procedurally building attributes cuts down on overhead/mantinence.
i need re-implement __setattr__ standard behavior maintained, special attributes, value get/set outside object.
i have seen examples of re-implementing __setattr__ on stack overflow, seem missing something.
i don't think maintaining default functionality of setattr
here example:
externaldata = {'translatex':1.0,'translatey':1.0,'translatez':1.0} attrkeys = ['translatex','translatey','translatez'] class transform(object): def __getattribute__(self, name): print 'getting --->', name if name in attrkeys: return externaldata[name] else: raise attributeerror("no attribute named [%s]" %name) def __setattr__(self, name, value): print 'setting --->', name super(transform, self).__setattr__(name, value) if name in attrkeys: externaldata[name] = value myinstance = transform() myinstance.translatex # result: 1.0 # myinstance.translatex = 9999 myinstance.translatex # result: 9999 # myinstance.name = 'myname' myinstance.name # attributeerror: no attribute named [name] #
!
this worked me:
class transform(object): def __getattribute__(self, name): if name in attrkeys: return externaldata[name] return super(transform, self).__getattribute__(name) def __setattr__(self, name, value): if name in attrkeys: externaldata[name] = value else: super(transform, self).__setattr__(name, value)
however, i'm not sure route go.
if external operations time consuming (say, you're using disguise access database or config file) may give users of code wrong impression cost. in case should use method users understand initiating action, not looking @ data.
otoh if access quick, careful encapsulation of classes isn't broken. if you're doing @ maya scene data (pymel-style, or in this example) it's not big deal since time costs , stability of data more or less guaranteed. you'd want avoid scenario in example code posted: easy assume having set 'translatex' given value stay put, in fact there lots of ways contents of outside variables messed with, preventing being able know invariants while using class. if class intended throwaway use (say, syntax sugar lot of fast repetitive processing inside loop no other operations running) away - if not, internalize data instances.
one last issue: if have 'a lot of classes' have lot of boilerplate make work. if trying wrap maya scene data, read on descriptors (here's great 5-minute video). can wrap typical transform properties, example, this:
import maya.cmds cmds class mayaproperty(object): ''' in real implmentation you'd want support different value types, etc storing flags appropriate different commands.... ''' def __init__(self, cmd, flag): self.command = cmd self.flag = flag def __get__(self, obj, objtype): return self.command(obj, **{'q':true, self.flag:true} ) def __set__(self, obj, value): self.command(obj, **{ self.flag:value}) class xformwrapper(object): def __init__(self, obj): self.object = obj def __repr__(self): return self.object # command work on string name of object translation = mayaproperty(cmds.xform, 'translation') rotation = mayaproperty(cmds.xform, 'rotation') scale = mayaproperty(cmds.xform, 'scale')
in real code you'd need error handling , cleaner configuration see idea.
the example linked above talks using metaclasses populate classes when have lots of property descriptors configure, route go if don't want worry boilerplate (though have minor startup time penalty - think that's 1 of reasons notorious pymel startup crawl...)
Comments
Post a Comment