Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to alias method names in python?

Hi I've written the following code:

class Car:
  def __init__(self):
    self._create_aliases()

  def take_to_repair_shop(self):
     pass

  def drive_to_the_beach(self):
     pass

  def _create_aliases(self):
     Car.repair = Car.take_to_repair_shop
     Car.beach = Car.drive_to_the_beach

I want the user to have the option of using the longer, clearer names, or the shorter names. I made _create_aliases into an instance method so that it could be overriden.

Are there any problems with this? Is this a good design pattern?

like image 519
user553965 Avatar asked Jan 12 '14 08:01

user553965


1 Answers

You don't need to do it in __init__:

class Car:

    def take_to_repair_shop(self):
        pass

    def drive_to_the_beach(self):
        pass

    repair = take_to_repair_shop # <---
    beach = drive_to_the_beach   # <---

Example:

>>> car = Car()
>>> car.repair()
>>> car.take_to_repair_shop()
>>>
like image 168
falsetru Avatar answered Sep 23 '22 17:09

falsetru