c++ using-keyword in python

Issue

In c++ you generally can do this:

using LCN = longNamespace::sub::subsub::LongClassName

so you don’t have to write a long classname everytime. Is there a similar keyword or workaround in python for static classmethods.

class MyPythonClassWithLongName:
    @staticmethod
    def foo():
        pass

    def bar():
        # instead of
        MyPythonClassWithLongName.foo()
        # i want to write
        LCN.foo()

So that I can write LCN.foo() instead of MyPythonClassWithLongName.foo()?

Solution

Sure: classes in Python are just objects, so you can reassign them freely to a new name:

LCN = MyPythonClassWithLongName
LCN.foo()

Whether that’s recommended for readability is another matter.

Answered By – Konrad Rudolph

Answer Checked By – Terry (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.