[Baypiggies] Fwd: A beginner's question on python class

Shannon -jj Behrens jjinux at gmail.com
Mon Feb 20 23:41:23 CET 2012


 * When you write "self.foo", Python will look for foo in the instance and
in the class hierarchy.
 * When you write "foo", Python will look for local variables, lexically
scoped variables (i.e. variables in outer functions), global variables, and
finally builtins.
 * If you use a class, function, etc. from library A, it's up to library A
to import all the things it needs.
 * Each file is implicitly a module with its own namespace.  This isn't the
case in other languages like Ruby.

Best Regards,
-jj

On Mon, Feb 20, 2012 at 2:02 PM, Yiou Li <liyiou at gmail.com> wrote:

> 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()
>
> ____________________________________
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>



-- 
In this life we cannot do great things. We can only do small things with
great love. -- Mother Teresa
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20120220/e9d8c0fc/attachment.html>


More information about the Baypiggies mailing list