How Can I Make An Alias To A Non-function Member Attribute In A Python Class?
Solution 1:
This can be solved in exactly the same way as with class methods. For example:
classDummy:
def__init__(self):
self._x = 17 @propertydefx(self):
return self._x
@x.setterdefx(self, inp):
self._x = inp
@x.deleterdefx(self):
del self._x
# Alias
xValue = x
d = Dummy()
print(d.x, d.xValue)
#=> (17, 17)
d.x = 0print(d.x, d.xValue)
#=> (0, 0)
d.xValue = 100print(d.x, d.xValue)
#=> (100, 100)
The two values will always stay in sync. You write the actual property code with the attribute name you prefer, and then you alias it with whatever legacy name(s) you need.
Solution 2:
You can provide a __setattr__
and __getattr__
that reference an aliases map:
classDummy:
aliases = {
'xValue': 'x',
'another': 'x',
}
def__init__(self):
self.x = 17def__setattr__(self, name, value):
name = self.aliases.get(name, name)
object.__setattr__(self, name, value)
def__getattr__(self, name):
if name == "aliases":
raise AttributeError # http://nedbatchelder.com/blog/201010/surprising_getattr_recursion.html
name = self.aliases.get(name, name)
returnobject.__getattribute__(self, name)
d = Dummy()
assert d.x == 17assert d.xValue == 17
d.x = 23assert d.xValue == 23
d.xValue = 1492assert d.x == 1492
Solution 3:
What are you going to do when half your users decide to use d.x
and the other half d.xValue
? What happens when they try to share code? Sure, it will work, if you know all the aliases, but will it be obvious? Will it be obvious to you when you put away your code for a year?
In the end, I think this kind of niceness or luxury is an evil trap that will eventually cause more confusion than good.
It's mostly because my scripting API is used across multiple subsystems & domains, so the default vocabulary changes. What's known as "X" in one domain is known as "Y" in another domain.
You could make aliases with properties this way:
classDummy(object):def__init__(self):
self.x=1@propertydefxValue(self):
returnself.x
@xValue.setter
defxValue(self,value):
self.x=value
d=Dummy()
print(d.x)
# 1
d.xValue=2
print(d.x)
# 2
But for the reasons mentioned above, I don't think this is a good design. It makes Dummy harder to read, understand and use. For each user you've doubled the size of the API the user must know in order to understand Dummy.
A better alternative is to use the Adapter design pattern. This allows you to keep Dummy nice, compact, succinct:
classDummy(object):def__init__(self):
self.x=1
While those users in the subdomain who wish to use a different vocabulary can do so by using an Adaptor class:
classDummyAdaptor(object):def__init__(self):
self.dummy=Dummy()
@propertydefxValue(self):
returnself.dummy.x
@xValue.setter
defxValue(self,value):
self.dummy.x=value
For each method and attribute in Dummy, you simply hook up similar methods and properties which delegate the heavy lifting to an instance of Dummy.
It might be more lines of code, but it will allow you to preserve a clean design for Dummy, easier to maintain, document, and unit test. People will write code that makes sense because the class will restrict what API is available, and there will be only one name for each concept given the class they've chosen.
Solution 4:
You could use some of ideas shown in the ActiveState Python recipe titled Caching and aliasing with descriptors. Here's a concise version of the code shown there which provides the functionality you seek.
Edit: A class containing Alias
attributes could be made to automatically delete any associated target attributes when you del
one (and vice-versa). The code for my answer now illustrates one easy way this could be done using a convenient class decorator which adds a custom __delattr__()
to do the specialized deletion management when attribute Alias's
could be involved.
classAlias(object):
""" Descriptor to give an attribute another name. """def__init__(self, name):
self.name = name
def__get__(self, inst, cls):
if inst isNone:
return self # a class attribute reference, return this descriptorreturngetattr(inst, self.name)
def__set__(self, inst, value):
setattr(inst, self.name, value)
def__delete__(self, inst):
delattr(inst, self.name)
defAliasDelManager(cls):
""" Class decorator to auto-manage associated Aliases on deletion. """def__delattr__(self, name):
""" Deletes any Aliases associated with a named attribute, or
if attribute is itself an Alias, deletes the associated target.
"""super(cls, self).__delattr__(name) # Use base class' method.for attrname indir(self):
attr = getattr(cls, attrname)
ifisinstance(attr, Alias) and attr.name == name:
delattr(cls, attrname)
setattr(cls, '__delattr__', __delattr__)
return cls
if __name__=='__main__':
@AliasDelManagerclassDummy(object):
def__init__(self):
self.x = 17
xValue = Alias('x') # create an Alias for attr 'x'
d = Dummy()
assert d.x == 17assert d.xValue == 17
d.x = 23assert d.xValue == 23
d.xValue = 1492assert d.x == 1492assert d.x is d.xValue
del d.x # should also remove any associated Aliasesassert'xValue'notindir(d)
print('done - no exceptions were raised')
Solution 5:
This function takes a attribute name as a param and return a property that work as an alias for getting and setting.
defalias_attribute(field_name: str) -> property:
"""
This function takes the attribute name of field to make a alias and return
a property that work to get and set.
"""
field = property(lambda self: getattr(self, field_name))
field = field.setter(lambda self, value: setattr(self, field_name, value))
return field
Example:
>>> classA:
... name_alias = alias_attribute('name')
... def__init__(self, name):
... self.name = name
... a = A('Pepe')
>>> a.name
'Pepe'>>> a.name_alias
'Pepe'>>> a.name_alias = 'Juan'>>> a.name
'Juan'
Post a Comment for "How Can I Make An Alias To A Non-function Member Attribute In A Python Class?"