Friday, May 6, 2011

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?

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

    Otherwise whateverName = VeryLongClassName should do the trick.

    Ross Rogers : The name isn't that long, but I want it to be exactly 1 character when I use the class in the interactive session.
  • C = MyReallyBigClassNameWhichIHateToType
    
    S.Lott : Works well, but leads to confusion on the long run. If you hate to type it, why not just fix the name once and forever?
    Ross Rogers : I was exaggerating. The original name is 7 characters long, but I truly want the interactive name to be 1 character long.
  • You can simply do:

    ShortName = MyReallyBigClassNameWhichIHateToType
    

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

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

    from modulename import ReallyLongNameWhichIHateToType as FriendlyName
    
    Jason Baker : You beat me to it!

0 comments:

Post a Comment