Join up (it's free!) whether you're just starting out or you're crazy advanced. Get tips from people who know what they're talking about, without being treated like the playground smelly kid. Because seriously, your school classes didn't teach you anything you wanted to know.
My file structure looks like this, this isn't a real app, this is just for learning purposes. I have a class file
person.py
which contains a Person class. I want to use in my script file
script.py
that class. The two files are in the same folder. I just am executing the script.py in terminal. How would I link that Person class in person.py to my script.py file to use the Person class.
Traceback (most recent call last):
File "script.py", line 3, in <module>
shane = Person('Shane Perreault', 'shaneprrlt@gmail.com')
NameError: name 'Person' is not defined
Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.
Basically, what it's saying that it is recommended you do:
import person # everything, even private methods beginning with "__" are imported
shane = person.Person('Shane Perreault', 'shaneprrlt@gmail.com') # should work
Also note that when writing Python modules, it isn't necessary to put each class within its own file. For example, if you want to encapsulate a person, you could write people.py and add all of the classes and methods related to people. Generally, as a rule, a module represents a single reusable unit.
Basically, what it's saying that it is recommended you do:
import person # everything, even private methods beginning with "__" are imported
shane = person.Person('Shane Perreault', 'shaneprrlt@gmail.com') # should work
Also note that when writing Python modules, it isn't necessary to put each class within its own file. For example, if you want to encapsulate a person, you could write people.py and add all of the classes and methods related to people. Generally, as a rule, a module represents a single reusable unit.
Its also worth noting that you can use the 'from' syntax to only import certain classes.
from random import randint
randint(1, 10)
would be the same as
import random
random.randint(1, 10)
The one that you'll be using most often is the top one. The reason for this is namespacing - the modules system means that you can have functions defined under the same name, using different classes. For example, the Person module could have a function called rename, as could a completely different module. Different modules can also have classes which have the same names and stuff too. It means that if you're making your code distributable, you know that other functions aren't going to overload yours, and vice versa (via naming conflicts). This is why it's generally frowned upon to import everything.
The use case for importing specific parts of modules you need into the global namespace (which means you don't have to put the module name beforehand) is that you can use it as shorthand throughout your code, which is good providing that you can be reasonably sure that naming conflicts won't occur. For example, it's unlikely that you, or anybody else, would define a function called randint in the global namespace so it would probably be okay to import it directly from the random module.
Oh, I was making a syntactical mistake. When I was using
import person
, I wasn't doing this when creating objects:
shane = person.Person()
. That was the mistake, fixed, and working great. Yeah, I was thinking the same thing after reading that
from person import *
was bad.
Also, in general. Modules are almost files with sets of related classes and functions right. So instead of having a folder with all related class files like in PHP, I have a file with all related classes and methods?
Furthermore, what if I have a module in a folder? How would I import it. What if my file structure was:
/main
+script.py
+/classes
++person.py
And I wanted to use person.py in my script.py. How would I import that?
Also, in general. Modules are almost files with sets of related classes and functions right. So instead of having a folder with all related class files like in PHP, I have a file with all related classes and methods?
Not almost, that's exactly what they are for. (:
ShanePerreault, on 06 June 2011 - 05:49 AM, said:
Furthermore, what if I have a module in a folder? How would I import it.
I know it sounds odd, but you're going to need an empty file named "__init__.py" in your /main/classes directory (as well as any subdirectory that you want to import classes from. Probably good to have one in /main too to make your entire folder an importable module). Once your directory structure looks like this:
you can treat the subdirectories as modules themselves and do any of the following below:
# This is if you hate yourself and want to type out the full path name every time.
import classes.person
shane = classes.person.Person('Shane Perreault', 'shaneprrlt@gmail.com')
or if that's awkward for you:
# This is okay unless you have multiple modules with the same name in different packages.
from classes import person
shane = person.Person('Shane Perreault', 'shaneprrlt@gmail.com')
and if you're even lazier:
# Only use this if you are certain that you won't have any conflicting "Person" classes.
from class.person import Person
shane = Person('Shane Perreault', 'shaneprrlt@gmail.com')
You could even do:
# This is pretty much the same as example 2, but slightly more verbose and less Pythonic.
import classes.person
person = classes.person
shane = person.Person('Shane Perreault', 'shaneprrlt@gmail.com')