Setting up a class

S

shaun

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?

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.

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

Any help would be appreciated.
Thanks,
Shaun
 
J

Joel Goldstick

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?

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 thetop of the class, I use the information I get from the database to fill itand I send it back) Is there anything I'm not doing right with this.

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??

Any help would be appreciated.
Thanks,
Shaun

You should take the smallest snippit of your code that shows your
problem and copy it here along with traceback where it fails. Java is
not python and vice versa, so some of your ideas will be confused for
a while
 
M

MRAB

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?

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.

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??

Any help would be appreciated.
Thanks,
Shaun
You should have a look as a Python tutorial to help you get used to the
language. It shouldn't take you long, provided that you remember that
Python isn't Java and don't try to write Java in Python. :)

Here's one you could look at:

Python for Java programmers
http://python4java.necaiseweb.org/Main/TableOfContents
 
D

Dave Angel

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)
 
C

Colin J. Williams

You should have a look as a Python tutorial to help you get used to the
language. It shouldn't take you long, provided that you remember that
Python isn't Java and don't try to write Java in Python. :)

Here's one you could look at:

Python for Java programmers
http://python4java.necaiseweb.org/Main/TableOfContents
The link given seems dated and incomplete - see "operator overloading".

Sorry I can't assist the OP.

Colin W.
 
D

Dennis Lee Bieber

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?

First mistake... IGNORE anything you know about Java classes.
(Including the one-class-per-file).
1) Is there anything I should know about passing in variables from another script to the class?

Normally you don't pass variables to the "class" -- you create an
INSTANCE of the class (and may pass some parameters to the __init__()
method.
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.

Python doesn't have "declarations" (other than "global" and related
in newer versions).

Normally one "return"s data from a method call. Otherwise one access
instance attributes using instance.attribute notation.
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??

"self" is the instance whose method is being called.

If you are trying to use a class for something that has no
instances, forget it, just package everything up as module and import
the module.
Any help would be appreciated.

Read the language reference manual, and the Python tutorial...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top