I have a file named schedule.py:
class SchedGen:
""" Class creates a pseudo random Schedule.
With 3/4 of total credit point required for graduation"""
def __init__(self, nof_courses=40):
random.seed()
self.courses = {}
self.nof_courses = nof_courses
for i in xrange(nof_courses):
self.courses[i] = college.Course(i)
self.set_rand_cred()
self.set_rand_chance()
self.set_rand_preq_courses()
def set_rand_cred(self):
""" Set random credit to courses uniformly between 2 and 6"""
temp_dict = self.courses.copy()
While importing content of schedule do I do import schedule like:
import schedule
If that's correct how can I access the function set_rand_cred(self) from SchedGen class?
You can either do
import schedule
schedgen = schedule.SchedGen()
schedgen.set_rand_red()
or
from schedule import SchedGen
schedgen = SchedGen()
schedgen.set_rand_red()
This link provides some information how Pythons import statement works.
The set_rand_cred() function is an instance function of the class, so you need to first create a class instance. To create a class instance, you need to be able to access the name of the class. You can do that in two ways.
Here's how to solve the problem using each way:
Importing the module:
import schedule
s = schedule.SchedGen()
s.set_rand_cred()
Importing the class from within the module and putting the class into the local namespace:
from schedule import SchedGen
s = SchedGen()
s.set_rand_cred()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With