funnyp0ny
funnyp0ny

Reputation: 298

Is it possible to avoid writing 'dot' each time when calling class method?

Sorry for somewhat unclear question. I'm actually wondering whether it's possible in Python not to mention class name, when you call class's methods iteratively? I mean to write instead of:

SomeClass.Fun1()
SomeClass.Fun2()
...
SomeClass.Fun100()

Something like:

DoWith SomeClass:
    Fun1()
    Fun2()
    ...
    Fun100()

?

Upvotes: 3

Views: 363

Answers (2)

Marcus Müller
Marcus Müller

Reputation: 36442

There are several methods to achieve that (from SomeClass import *, locals().update(SomeClass.__dict__())), but what you're trying is not really logical:

In 90% of cases you're not calling static class methods, but member functions, which need a single instance to operate on. You do realize that the first, the self argument that you typically see on methods is important, because it gives you access to the instance's namespace. So even in methods, you use self.my_member instead of my_member. That's an important python concept, and you should not try to avoid it -- there's a difference between the local name space and the attributes of an instance.

What you can do, however, is having a short handle, without any overhead:

my_instance = SomeClass() #notice, this is an instance of SomeClass, not the class or type itself
__ = my_instance

that can save you a lot of typing. But I prefer clarity over saved typing (hell, vim has good autocompletion plugins for Python).

Upvotes: 3

farhawa
farhawa

Reputation: 10417

yes, just try from SomeClass import * (after moving SomeClass to an other file of course)

Upvotes: 1

Related Questions