0

I am working on a project that requires me to build several classes and subclasses in one file, and use them in a second file. I would like to ask how Python handles importing the first file into the second.

For instance, if i have a file my_classes.py:

class Myclass(object):
    pass

class Mysubclass(myclass):
    pass

will using the following code work:

from my_classes import Myclass

print Mysubclass

(where the print command is just an example of using Mysubclass), or do I need to import Mysubclass explicitly?

Thanks in advance!

LSchoon
  • 111
  • 1
  • 4
  • 1
    why not try this code and see if there is an error? – zhangxaochen Jan 17 '14 at 14:54
  • Seems like the obvious solution and indeed answers my question. However, I would like some background information on why Python behaves the way it does (e.g. why are subclasses not automatically loaded when the class is imported). – LSchoon Jan 17 '14 at 15:02

3 Answers3

2

This won't work. Python import statement doesn't care about subclasses. Actually, it doesn't care about anything. It does precisely what you tell it to do. "Explicit is better than implicit" is a popular saying in Python circles.

Here:

from my_classes import Myclass

You told Python to import only Myclass.

This will import both classes:

from my_classes import Myclass, Mysubclass

You can read how Python import works here.

Nigel Tufnel
  • 11,146
  • 4
  • 35
  • 31
  • Thanks! The link you provided goes a little beyond my comprehension, but answers the question I had. – LSchoon Jan 17 '14 at 15:05
0

Your subclass will not be available if you do it like this. You must import every object by itself.

Here a quick example

test_class.py class MyClass(object): def init(self): print self.class

class MySubClass(MyClass):
    def __init__(self):
        print self.__class__

test_class_import.py from test_class import MyClass

MyClass()
MySubClass()

##output##
<class 'test_class.MyClass'>
Traceback (most recent call last):
  File "test_class2.py", line 4, in <module>
    MySubClass()
NameError: name 'MySubClass' is not defined

but

from test_class import MyClass, MySubClass
MyClass()
MySubClass()

##output##
<class 'test_class.MyClass'>
<class 'test_class.MySubClass'>
Alex Kir
  • 77
  • 2
  • 8
0

Every module has a namespace.

A namespace is a mapping from variable names to values (Python objects).

The statement

import my_classes

makes the my_classes namespace accessible from the current module by placing the variable name my_classes in the current module's namespace. You can then access values from my_classes with the syntax

my_classes.variable

So, for example:

import my_classes
print my_classes.Mysubclass
print my_classes.MyClass

If that is too much typing, I suggest

import my_classes as MC
print MC.Mysubclass
print MC.MyClass

you could also do

from my_classes import Mysubclass, MyClass

but this form of import is discouraged by some Python experts.

from my_classes import Mysubclass, MyClass

loads the entire module my_class but only places the variable names Mysubclass and MyClass in the current module's namespace. They point to the same values as do the variables of the same name in the my_classes namespace. You don't get access to anything else from the my_class module's namespace.

Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677