Sorry for probably a “dumb” question, but I am new to Python. In some of the books with example projects I am investigating, I see a CLASS from a separate .py file (eg: Square.py - containing the class called “Square”, are imported into the main_py run file as, eg: “from Square import *.” Square is the only class in the Square.py file, yet if I try to import as follows: “import Square”, I get an error. It only works if I import as follows: "from Square import * - Why is import sometimes: “import pygames” and other times "from Square import * - ? Thank you anyone can explain this to me.
Hi,
the same way you do any other object (variables, functions, strings, etc). Assuming you have two modules. One name A
and the other name B
.
If there is a class name MyClass
in module A
, and you want to import it to module B
, you can do:
from A import MyClass
or
import A
then, to reference the class, you would prefix it with its module, as in:
A.MyClass
This is the same thing that you do with imported library modules including the numpy
module.
import numpy as np
np.sqrt(25)
In Python, when you import a module, you get the module. Unlike some other languages, there is no special relationship between modules and classes. When you write a module, you’re allowed to have a class with the same name. You’re also allowed to have just one class with a different name. Or many classes, which may or may not have the module’s name. Or no classes at all.
The Square
class is only one component of your Square
module, even if it’s the only visible part. It’s the same as how a list with one string element, like ['example']
, is a completely different kind of thing from that string itself, 'example'
.
The things contained in a module are attributes of the module, so you access them using the dot syntax. If you have source code Square.py
and it defines a class Square
, then import Square
gives you a module named Square
, which has a Square
attribute (the class), which you access like: Square.Square
.
Well, no, you have many other options.
This code means: load the Square
module, but don’t give it the name Square
. Instead, look up every attribute that the module has, and set each of their names directly. So, if there’s a class Square
, then Square
becomes a name for that class. If there’s a function resize
, then resize
becomes a name for that function. If there’s an integer assigned like NUMBER_OF_SIDES = 4
, then NUMBER_OF_SIDES
becomes a name for that integer. Name by name, you get the same names in the current code, as were in the code you’re importing.
This is a dangerous tool. You don’t get to control what names are assigned; the module does. If you were already using one or more of those names, well, they get replaced. If the module shadows a built-in name, now your code does too.
If you only want the Square
class, you can say that: from Square import Square
.
If you want a few names, but want to keep them organized inside the module, you can just import the module - import Square
- and access the contents explicitly: my_square = Square.Square()
.
Thank you. I appreciate your clarification and time.
Thank you for clarifying this for me. I greatly appreciate it.