Hi All,
I created a python class by inherating from and existing class: myPythonScript.py from standardClass import ClassX Class myClass(ClassX) myVariable _______________________________________________ Baypiggies mailing list [hidden email] To change your subscription options or unsubscribe: http://mail.python.org/mailman/listinfo/baypiggies |
On Sun, Feb 19, 2012 at 6:57 PM, Yiou Li <[hidden email]> wrote: Hi All, What's your question? By the way, class should be lower case, and you're forgetting a semicolon. You might be better off buying a book on Python or reading a tutorial. That way, you can ask us the really hard questions ;)
Happy Hacking! -jj In this life we cannot do great things. We can only do small things with great love. -- Mother Teresa _______________________________________________ Baypiggies mailing list [hidden email] To change your subscription options or unsubscribe: http://mail.python.org/mailman/listinfo/baypiggies |
On Sun, Feb 19, 2012, Shannon -jj Behrens wrote:
> On Sun, Feb 19, 2012 at 6:57 PM, Yiou Li <[hidden email]> wrote: >> >> I created a python class by inherating from and existing class: >> >> myPythonScript.py >> >> from standardClass import ClassX >> >> Class myClass(ClassX) >> myVariable > > What's your question? By the way, class should be lower case, and you're > forgetting a semicolon. s/semicolon/colon/ Also, OP should read PEP 8 to learn about proper formatting: the One True indent is four spaces. http://www.python.org/dev/peps/pep-0008/ -- Aahz ([hidden email]) <*> http://www.pythoncraft.com/ "Do not taunt happy fun for loops. Do not change lists you are looping over." --Remco Gerlich _______________________________________________ Baypiggies mailing list [hidden email] To change your subscription options or unsubscribe: http://mail.python.org/mailman/listinfo/baypiggies |
In reply to this post by Shannon -jj Behrens
Let me also add: I think JJ means that you forgot to put the colon (not semicolon) at the end of your class definition syntax. Here is a sample of code that demonstrates. Try this:
class ClassX(object): pass class MyClass(ClassX): my_variable = "Some Content" my_class = MyClass() print my_class.my_variable There are some great books and tutorials that help teach you to use Python... G Things which matter most must never be at the mercy of things which matter least. -- Goethe _______________________________________________ Baypiggies mailing list [hidden email] To change your subscription options or unsubscribe: http://mail.python.org/mailman/listinfo/baypiggies |
In reply to this post by Yiou Li
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 [hidden email] To change your subscription options or unsubscribe: http://mail.python.org/mailman/listinfo/baypiggies |
In reply to this post by Aahz
-jj
-- In this life we cannot do great things. We can only do small things with great love. -- Mother Teresa _______________________________________________ Baypiggies mailing list [hidden email] To change your subscription options or unsubscribe: http://mail.python.org/mailman/listinfo/baypiggies |
In reply to this post by Yiou Li
* 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 <[hidden email]> wrote: Thanks for the reply to my "imcompleted" question. In this life we cannot do great things. We can only do small things with great love. -- Mother Teresa _______________________________________________ Baypiggies mailing list [hidden email] To change your subscription options or unsubscribe: http://mail.python.org/mailman/listinfo/baypiggies |
In reply to this post by Yiou Li
On Mon, Feb 20, 2012 at 2:02 PM, Yiou Li <[hidden email]> wrote:
> 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. Short answer: yes, you have to import random even if another module you imported already imports random. Long answer: "import" is similar to "#include", but not identical. In general, each .py or .pyc file represents a module, and each import statement makes the contents of one module available from within the importing module. However, unlike #include, the import command doesn't add the contents of the other module to the importing module's namespace, but rather creates a reference to it that behaves similarly to a C++ namespace (e.g. import random; random.shuffle(...)). One consequence of this is that if you want to import modules A and B, and module B already imports module A, you need to either explicitly import both modules, or you have to refer to module B to get to module A (i.e. B.A.function instead of A.function). Most of the time, importing both modules is the preferred solution. Note that importing a module multiple times will NOT cause multiple copies of it to be made in memory; the Python interpreter is smart enough to recognize that a module has already been imported and will just create a new reference to it. Disclaimer: this is a highly simplified explanation, and omits several nuances of how importing works in Python. For 99% of Python development, however, you don't need to worry about those nuances. Hope this helps. -- Lincoln Peters <[hidden email]> _______________________________________________ Baypiggies mailing list [hidden email] To change your subscription options or unsubscribe: http://mail.python.org/mailman/listinfo/baypiggies |
Thanks to all for your replies!
Leo On Mon, Feb 20, 2012 at 3:45 PM, Lincoln Peters <[hidden email]> wrote: > On Mon, Feb 20, 2012 at 2:02 PM, Yiou Li <[hidden email]> wrote: >> 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. > > Short answer: yes, you have to import random even if another module > you imported already imports random. > > Long answer: "import" is similar to "#include", but not identical. In > general, each .py or .pyc file represents a module, and each import > statement makes the contents of one module available from within the > importing module. However, unlike #include, the import command > doesn't add the contents of the other module to the importing module's > namespace, but rather creates a reference to it that behaves similarly > to a C++ namespace (e.g. import random; random.shuffle(...)). One > consequence of this is that if you want to import modules A and B, and > module B already imports module A, you need to either explicitly > import both modules, or you have to refer to module B to get to module > A (i.e. B.A.function instead of A.function). Most of the time, > importing both modules is the preferred solution. > > Note that importing a module multiple times will NOT cause multiple > copies of it to be made in memory; the Python interpreter is smart > enough to recognize that a module has already been imported and will > just create a new reference to it. > > Disclaimer: this is a highly simplified explanation, and omits several > nuances of how importing works in Python. For 99% of Python > development, however, you don't need to worry about those nuances. > > > Hope this helps. > > > -- > Lincoln Peters > <[hidden email]> > _______________________________________________ > Baypiggies mailing list > [hidden email] > To change your subscription options or unsubscribe: > http://mail.python.org/mailman/listinfo/baypiggies Baypiggies mailing list [hidden email] To change your subscription options or unsubscribe: http://mail.python.org/mailman/listinfo/baypiggies |
Free forum by Nabble | Edit this page |