I'm trying create the simplest code to handle aliases for kwargs keywords. For example, in matplotlib, color='red', can also be tagged as c='red'. The following code works, but is there a better way?
I was considering developing a class/structure that knows the keyword, the alias, the default value, and the variable they are linked to. A list or dictionary of these would be declared in the function and a single call would parse the kwargs.
class Cylinder():
def __init__(self,d:float,l:float):
"""
d = diameter
l = length
"""
self.d:float = d
self.l:float = l
#Default values
self.color:str = 'black'
self.transparency:float = 0.0
return
def Plot(self, **kwargs):
"""
Optional parameters:
color or c default 'black'
transparency or tr default 0.0
"""
if 'color' in kwargs:
self.color = kwargs['color']
elif 'c' in kwargs:
self.color = kwargs['c']
if 'transparency' in kwargs:
self.transparency = kwargs['transparency']
elif 'tr' in kwargs:
self.transparency = kwargs['tr']
print("Plotting....color = ", self.color, ' transparency = ', self.transparency)
return
You can create an utility function to handle that: