Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Setting up a class

Reply
Thread Tools

Setting up a class

 
 
shaun
Guest
Posts: n/a
 
      09-06-2012
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
 
Reply With Quote
 
 
 
 
Joel Goldstick
Guest
Posts: n/a
 
      09-06-2012
On Thu, Sep 6, 2012 at 8:00 AM, shaun <(E-Mail Removed)> 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?
>
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list


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


--
Joel Goldstick
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      09-06-2012
On 06/09/2012 13:00, 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?
>
> 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

 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      09-06-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

 
Reply With Quote
 
Colin J. Williams
Guest
Posts: n/a
 
      09-06-2012
On 06/09/2012 8:20 AM, MRAB wrote:
> On 06/09/2012 13:00, 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?
>>
>> 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
>

The link given seems dated and incomplete - see "operator overloading".

Sorry I can't assist the OP.

Colin W.
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      09-06-2012
On Thu, 6 Sep 2012 05:00:32 -0700 (PDT), shaun
<(E-Mail Removed)> declaimed the following in
gmane.comp.python.general:

>
> 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...
--
Wulfraed Dennis Lee Bieber AF6VN
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Library class loader (setting system class loader parent) Karsten Wutzke Java 0 06-29-2007 04:44 AM
Setting class variable from parent class? Marco Ruby 4 04-03-2006 11:44 PM
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments