Importing class from another file in python - I know the fix, but why doesn't the original work?
Question: I can make this code work, but I am still confused why it won't work the first
way I tried.

I am practicing python because my thesis is going to be coded in it (doing
some cool things with Arduino and PC interfaces). I'm trying to import a class
from another file into my main program so that I can create objects. Both
files are in the same directory. It's probably easier if you have a look at
the code at this point.

    
    
    #from ArduinoBot import *
    #from ArduinoBot import ArduinoBot
    import ArduinoBot
    
    # Create ArduinoBot object
    bot1 = ArduinoBot()
    
    # Call toString inside bot1 object
    bot1.toString()
    
    input("Press enter to end.")
    

Here is the very basic ArduinoBot class

    
    
    class ArduinoBot:
    
    def toString(self):
        print ("ArduinoBot toString")
    

Either of the first two commented out import statements will make this work,
but not the last one, which to me seems the most intuitive and general.
There's not a lot of code for stuff to go wrong here, it's a bit frustrating
to be hitting these kind of finicky language specific quirks when I had heard
some many good things about Python. Anyway I must be doing something wrong,
but why doesn't the simple 'import ClassName' or 'import FileName' work?

Thank you for your help.


Answer: consider a file (`example.py`):

    
    
    class foo(object):
        pass
    
    class bar(object):
        pass
    
    class example(object):
        pass
    

Now in your main program, if you do:

    
    
    import example
    

what should be imported from the file `example.py`? Just the class `example`?
should the class `foo` come along too? The meaning would be too ambiguous if
`import module` pulled the whole module's namespace directly into your current
namespace.

The idea is that namespaces are wonderful. They let you know where the
class/function/data came from. They also let you group related things together
(or equivalently, they help you keep unrelated things separate!). A module
sets up a namespace and you tell python exactly how you want to bring that
namespace into the current context (namespace) by the way you use `import`.

`from ... import *` says -- bring everything in that module directly into my
namespace.

`from ... import ... as ...` says, bring only the thing that I specify
directly into my namespace, but give it a new name here.

Finally, `import ...` simply says bring that module into the current
namespace, but keep it separate. This is the most common form in production
code because of (at least) 2 reasons.

  * It prevents name clashes. You can have a local class named `foo` which won't conflict with the `foo` in `example.py` \-- You get access to that via `example.foo`
  * It makes it easy to trace down which module a class came from for debugging.

consider:

    
    
    from foo import *
    from bar import *
    a = AClass() #did this come from foo?  bar? ... Hmmm...
    

* * *

In this case, to get access to the class `example` from `example.py`, you
could also do:

    
    
    import example
    example_instance = example.example()
    

but you can also get `foo`:

    
    
    foo_instance = example.foo()
    

