Eugene
Eugene

Reputation: 598

alias for python class in another module

For example:

I have a module A what contains class CLASS, also I have module B what uses class CLASS as parameter in its methods and I have module C what uses module B. So I should import A.CLASS to make it possible to pass CLASS as parameter to methods from module B. But if I'll put module A to another packages D.E.F... I'll get a really long import string

What is best way to make alias for CLASS in module B, to using B.someMethod(B.CLASS) and module C knows nothing about module A.

Now I import A.Class in module B, and then use import B.CLASS in module C

.# File: a.py

class Klass(object):
    pass

.# File b.py

from a import Klass

def somemethod(foo):
    pass

.# File c.py

from b import somemethod
from b import Klass

somemethod(Klass.field)

Upvotes: 0

Views: 327

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174662

I assume you are trying to describe this situation, and I don't see any issue here:

I have a module A what contains class CLASS

# File: a.py

class Klass(object):
    pass

I have module B what uses class CLASS as parameter in its methods

# File b.py

from a import Klass

def somemethod(foo):
    pass

somemethod(Klass)

I have module C what uses module B. So I should import A.CLASS to make it possible to pass CLASS as parameter to methods from module B.

# File c.py

from b import somemethod
from a import Klass

somemethod(Klass)

Now I import A.Class in module B, and then use import B.CLASS in module C

Just import A.Class in B, since B.Class is the same as A.Class

Upvotes: 1

Related Questions