Setting up a class

Dave Angel d at davea.name
Thu Sep 6 09:41:33 EDT 2012


On 09/06/2012 08:00 AM, shaun wrote:
> Hi all,
>
> So I'm trying to to OO a script which is currently in place on work. It connects to the database and makes multiple strings and sends them to a server.
>
> But I'm having major problems since I am new to python I keep trying to do it as I would do it in Java but classes seem to be very different. I was wondering could someone answer a few questions? 
>
> 1) Is there anything I should know about passing in variables from another script to the class?

Things don't get passed to a class.  They get passed to an initializer
(and to the constructor, but you've probably never used one of those),
or to methods of the class.  There's only one script in a given program,
the other source files are modules.  If code in one module wants to use
code  or data from another module, there are two general ways to do it. 
One is to use the "from module1 import global1, func2" syntax.  In this
case, they effectively become globals of the current file.  And the
other is to use   module1.func2()   syntax, where you qualify where to
find the function.

As others have pointed out to you,   "from xxx import *"   is very
risky, as you make everything global, and the reader can no longer tell
where a particular symbol comes from.  Further, any name collisions are
silently dealt with, on the assumption that you REALLY know what you're
doing.

>
> 2) When I'm passing variables back to the script they seem to come back blank as if I haven't done it correctly (I declare the empty variable at the top of the class, I use the information I get from the database to fill it and I send it back) Is there anything I'm not doing right with this.
You don't pass variables back to a script.  If you mean return a value
from a function, then say so.  Note that a return statement takes a
single object, but that object could very well be a tuple.  So it's
perfectly reasonable for a function to return as:

          return  first, second

And the caller might have done something like:
        a, b = myfunc()

first will go into a,and second will go into b.  This subtlety is
because the expression   first,second   is a standard way to specify a
tuple of size 2.  And tuple unpacking can be done similarly with  his,
hers = expression.  No restriction with size of 2, but you do want the
same number of items in both places.

There is no need to declare any variable, anywhere.  If it needs an
initial value, then assign it.  Further, things 'at the top of the
class' are class attributes, not just variables.  How can you ask if
there's anything you're not doing right when you post no code with your
vague question?

> 3)When I want to use a method from a class in another class method it never seems to work for me, I have a feeling this is to do with "self" but im not too sure??

If one class method needs to call another method of the same class,
you'll usually want to use the self object:
class ....
       def  method1(self):
              arg1 = 42
             self.method2(arg1)
       def method2(self, value);
             ...dosomething interesting...

Naturally, when it's a method of some other class, or when you need a
different instance of the same class, then you'd better have an instance
to use with it.
           someinstance.method2(arg1)



-- 

DaveA




More information about the Python-list mailing list