python - 2 names for a same attribute -


i know if there way "link" 2 attributes of class or give 2 names same attribute?

for example, i'm working on script create triangle data given users. triangle abc. sides of triangle ab, bc , ca. triangle has got these 3 attributes (self.ab, self.bc, self.ca). ab = ba allow users print myinstance.ba instead of print myinstance.ab.

so thought create attribute self.ab , property ba (which return self.ab). work fine when try print myinstance.ba instead of print myinstance.ab i'm greedy...

i allow users myinstance.ba = 5 instead of myinstance.ab = 5 , when doing edit attribute ab.

is there way ?

python properties can have setters. need is

class foo(object):    @property   def ba(self):       return self.ab    @ba.setter   def ba(self, value):       self.ab = value 

and insipred @amccormack's answer, if can rely on order of attribute names, works more generically e.g. edges bc, cd:

  class foo(object):         def __init__(self):           self.ab = 100        def __getattr__(self, name):           return getattr(self, "".join(sorted(name)))        def __setattr__(self, name, value):           super(foo, self).__setattr__("".join(sorted(name)), value)     f = foo()    print f.ba   f.ba = 200   print f.ba 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -