Ross Rogers
Ross Rogers

Reputation: 24240

How do you alias a python class to have another name without using inheritance?

If I have a python class, how can I alias that class-name into another class-name and retain all it's methods and class members and instance members? Is this possible without using inheritance?

e.g. I have a class like:

class MyReallyBigClassNameWhichIHateToType:
    def __init__(self):
         <blah>
    [...]

I'm creating an interactive console session where I don't want my users' fingers to fall off while instantiating the class in the interactive sessions, so I want to alias that really long class name to something tiny like 'C'. Is there an easy way to do this without inheritance?

Upvotes: 56

Views: 50968

Answers (5)

dF.
dF.

Reputation: 75795

You can simply do:

ShortName = MyReallyBigClassNameWhichIHateToType

A class in Python is just an object like any other, and can have more than one name.

Upvotes: 19

Hello Human
Hello Human

Reputation: 584

Simple name assignment approach works but has one disadvantage that might be important in some cases: the alias name will be the same as the name of the "base" class because of the __name__ property.

class C(object):
    pass

D = C

print(C.__name__)  # 'C'
print(D.__name__)  # 'C' again

For example, if you create custom exception and then another one that assigning the first one you will get the name of this "parent" exception every time no matter which one of them you raise and this should probably confuse a user:

class CustomBaseException(Exception):

    def __init__(self, operation):
        super(CustomBaseException, self).__init__()
        self.operation = operation

    def __str__(self):
        return f"Invalid operation '{self.operation}'"


OperationException = CustomBaseException

raise OperationException('readd')

output:

Traceback (most recent call last):
  File "<input>", line 12, in <module>
CustomBaseException: Invalid operation 'readd'

So a solution would be to actually subclass the class:

class CustomBaseException(Exception):

    def __init__(self, operation):
        super(CustomBaseException, self).__init__()
        self.operation = operation

    def __str__(self):
        return f"Invalid operation '{self.operation}'"


class OperationException(CustomBaseException):
    pass


raise OperationException('delite')

output:

Traceback (most recent call last):
  File "<input>", line 14, in <module>
OperationException: Invalid operation 'delite'

Upvotes: 7

James Emerton
James Emerton

Reputation: 4259

Also, if you're importing the name from another module...

from modulename import ReallyLongNameWhichIHateToType as FriendlyName

Upvotes: 65

SilentGhost
SilentGhost

Reputation: 319821

C = MyReallyBigClassNameWhichIHateToType

Upvotes: 111

Yuval Adam
Yuval Adam

Reputation: 165282

Refactor the name, no reason it should have a name that long.

Otherwise whateverName = VeryLongClassName should do the trick.

Upvotes: 12

Related Questions