[Baypiggies] Fwd: A beginner's question on python class
Yiou Li
liyiou at gmail.com
Mon Feb 20 23:02:10 CET 2012
Thanks for the reply to my "imcompleted" question.
Actually my question was:
I created a python class by inheriting from and existing class and use
this new class in my main script as below:
My question is, since I import a function "sample" from the library
"random" in myClass, do I have to import "sample" in my main script as
well?
--- I did a debugging experiment using the following code to see that
the library "random" was imported at myClassInstance.myFunction(),
therefore, the answer to my question is -- I don't have to import the
"random" library in the main script. And the import statement is just
like a #include statement in C.
Please correct me if I am wrong.
Best and thanks to your replies!!
Leo
myClassScript.py
_________________________________
from random import sample
class myClass():
myVariable = 1
def myFunction():
index = sample(...)
__________________________________
myMainScript.py
__________________________________
from myClassScript import myClass
def main():
myClassInstance = myClass()
myClassInstance.myFunction()
if __name__ == '__main__':
main()
____________________________________
More information about the Baypiggies
mailing list