How can I make an alias to a non-function member attribute in a Python class?

后端 未结 6 1701
Happy的楠姐
Happy的楠姐 2021-02-01 05:05

I\'m in the midst of writing a Python library API and I often run into the scenario where my users want multiple different names for the same functions and variables.

If

6条回答
  •  暖寄归人
    2021-02-01 05:38

    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:

    class Dummy(object):
       def __init__(self):
          self.x=1
       @property
       def xValue(self):
          return self.x
       @xValue.setter
       def xValue(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:

    class Dummy(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:

    class DummyAdaptor(object):
       def __init__(self):
          self.dummy=Dummy()
       @property
       def xValue(self):
          return self.dummy.x
       @xValue.setter
       def xValue(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.

提交回复
热议问题