Reputation: 1974
For larger programs, in order to be more organized, I have been looking into dividing my code up into different .py
files and having the main file that calls upon those files when needed. I have looked around and seen lots of remarks about creating a directory and a SystemPath
for Python. Are those reasonable options for a program that could be distributed between a few computers? As a test, I tried to assemble an example:
This is the class named grades
in the same directory as main
class student:
def __init__(self):
self.name = ""
self.score = 0
self.grade = 0
def update(self,name,score,grade):
self.score = score
self.name = name
self.grade = grade
print self.score,self.name,self.grade
s = student()
s.update(name,score,grade)
This is my main script currently:
from grades import score
import random
name = 'carl'
score = random.randrange(0,100)
grade = 11
s = student()
s.score(name,score,grade)
There are some questions I have generally about this method:
# way that works
s = student()
s.update(name,score,grade)
# incorrect way
student.update(name,score,grade)
Thank you for your time and thought towards my question.
Upvotes: 1
Views: 9514
Reputation: 3782
First, you can use from module import *
to import everything like:
hello.py:
def hello():
print 'hello'
def bye():
print 'Bye'
main.py:
from hello import *
hello()
bye()
But it's not a good way, if you have two files, two functions have the same name, so use
from hello import hello, bye
hello()
bye()
is better, it an example for function ,as same as class.
Third before Second, student is a class, so you have to use an instance object to use the function which with self parameter. If you want to use student.function, the function must be a static method like this:
class Person:
def __init__():
pass
@staticmethod
def Count():
return 1
print Person.Count()
Second, you can import the function in a class file which is independent of the class.
Upvotes: 1
Reputation: 107337
- Is there a way to import all from different file or do i need to specify each individual class?
the answer is yes , as python import
statement use sys.path
(A list of strings that specifies the search path for modules ) you need to add the patht of your modules in sys.path
, for example if you want to interact between different computers you can put your modules in public
folder and add the path of folder to sys.path
:
import sys
sys.path.append( path to public )
- If i just had a function, is it possible to import it just as a function or can you only import via a class?
you just need to use from ... import function_name
.
- Why is it when i call upon a class in general i have to make a variable for it as in the example below?
for this question you just need to read the python Class objects
documentation :
Class objects support two kinds of operations: attribute references and instantiation.
Upvotes: 0
Reputation: 4912
You can import instance of student
from other script to main script like this:
from grades import s
# if your first script is called grades.py
import random
name = 'carl'
score = random.randrange(0,100)
grade = 11
# you can directly use it without initializing it again.
s.score(name,score,grade)
2.
If you have a function called test()
in grades.py, you can import it in this way:
from grades import test
# then invoke it
test()
3. This variable stands for the instance of class student. You need this instance to invoke the function inside.
Upvotes: 2
Reputation: 652
Generally, to divide the source code of a program, Python use module to do that, which corresponds to a *.py file. Then for your 3 questions:
import module_name.*
for a function, if it is a function in a class(member method, class method or static method) you can not only import the function, you should import class to use the method; if it is a function under module, you can separately import the function through import module_name.function_name
update is a member function of the student class, so you should use it through an instance. if it is a class method or static method, you can use it through the class name you wrote.
Upvotes: 2
Reputation: 303
1: Is there a way to import all from different file or do i need to specify each individual class?
You can use the "wildcard import", but you probably shouldn't. See Should wildcard import be avoided?
- If i just had a function, is it possible to import it just as a function or can you only import via a class?
Functions can be totally independent of classes in Python.
3.Why is it when i call upon a class in general i have to make a variable for it as in the example below?
You should read up on object-oriented programming. In the basic cases, you have to instantiate instances of a class in order to use that class's functionality. In your example, the class student describes what it means to be a student, and the statement
s = student()
creates a student and names it "s". I think this should be clear after reading a bit about object-oriented programming.
Upvotes: 1