Reputation: 43
This is my first question here in stackoverflow, so if I make one or two mistakes, please, let me now. Also, I'm not an English native speaker, so be prepared for some grammar mistakes as well.
The thing is that I'm a total newbie, and I am learning programming through python and javascript. Right now I've covered the essentials and I'm experimenting with my first app in python, so it's the first time that I practice with modules, inheritance and etc. And I have some doubts regarding the importation of methods/classes through various modules:
Class_1
). Then I write another class (e.g. Class_2
) in another Module. Class_2
inherits from Class_1
. At the same time, I have some methods from yet another Module imported at the Class_1.__init__
method level. When I write Class_2
, I have to import again in the __init__
method the methods imported in Class_1
or the inheritance implies also the methods imported at the Class_1
level?Example:
class Class_1(object):
def __init__(self, args):
from Module_5.Module_A import method_x
# Class definition...
class Class_2(Class_1):
def __init__(self, args):
from Module_5.Module_A import method_x again?
# Class definition...
method_x
) from another Module (again, in the __init__
method of each class). Is there some performance implication for the classes that don't use the method_x
if I import a method at the Module level, instead of at the class level? What is the 'usual' or the 'more pythonic' way (if there is such a think in this case), importing the method at the module level or at the class level when a) only one class in the module needs the imported method and when b) 1 < [number of classes that need the method] < [small percentage of classes in the Module] need the imported method?Upvotes: 4
Views: 11239
Reputation: 55469
As Gribouillis has mentioned it is customary to put all your import
statements at the start of the file.
You can put import
s inside class or function definitions, but there's no point, it makes the code harder to read, and there is no real benefit in doing so. (In fact, it can be slightly less efficient if several of your classes import from the same module, since you're needlessly giving the interpreter more work to do, but it's not a big deal because the interpreter always checks to see if a module is already loaded before it attempts to read it from disk).
One minor exception to this rule is if you have code that only gets run when the file is designed to be both imported as a module and run directly as a command-line script. Such files will normally have a
if __name__ == '__main__':
section at the end for code that only gets run when the file is run as a script. And that code may need a module (eg argparse
) that's not needed by the rest of the code. In that situation it's ok to put such additional imports at the top of that section rather than at the top of the file.
Whether you do
import Module_5.Module_A
or
from Module_5.Module_A import method_x
the whole of the Module_5.Module_A
module is imported, the difference is that the first form adds the name Module_5.Module_A
to your namespace, and the second form adds the name method_x
to your namespace. Doing from
imports is ok when you only need a few names from the module, but it makes the code harder to read because you can't tell just by looking at a name where it comes from. So it's generally better to do something like
import Module_5.Module_A as m5A
and then you can access its names like this:
m5A.method_x
and that way it's obvious that method_x
comes from m5A
.
You asked: "When I write Class_2
, I have to import again the methods imported in Class_1
or the inheritance implies also the methods imported at the Class_1 level
?". Yes, you don't need to explicitly import the first module unless you want to directly access its names. Let me illustrate with a simpler example.
Say we have 3 modules.
def f1(x):
return x
import m1
def f2(x):
return m1.f1(x) * 2
import m2
def f3(x):
return m2.f2(x) * 3
print(f3(1))
As you can see, m3.py
doesn't need to import m1.py
, unless you want to directly access m1.f1
, m2.f2
will be able to find m1.f1
due to the import
statement in m2.py
.
Upvotes: 0
Reputation: 2220
Usually one does not import names within the class' body. The custom is to write import statements near the top of the file
from Module_5.Module_A import method_x
class Class_1(object):
# Class definition...
class Class_2(Class_1):
# Class definition...
There is no significant performance advantage or penalty in doing so.
Upvotes: 4