Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28579 > unrolled thread

Setting up a class

Started byshaun <shaun.wiseman91@gmail.com>
First post2012-09-06 05:00 -0700
Last post2012-09-06 17:23 -0400
Articles 6 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Setting up a class shaun <shaun.wiseman91@gmail.com> - 2012-09-06 05:00 -0700
    Re: Setting up a class Joel Goldstick <joel.goldstick@gmail.com> - 2012-09-06 08:16 -0400
    Re: Setting up a class MRAB <python@mrabarnett.plus.com> - 2012-09-06 13:20 +0100
      Re: Setting up a class "Colin J. Williams" <cjw@ncf.ca> - 2012-09-06 09:41 -0400
    Re: Setting up a class Dave Angel <d@davea.name> - 2012-09-06 09:41 -0400
    Re: Setting up a class Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-09-06 17:23 -0400

#28579 — Setting up a class

Fromshaun <shaun.wiseman91@gmail.com>
Date2012-09-06 05:00 -0700
SubjectSetting up a class
Message-ID<a451e907-5167-4e3e-a7f2-d63ffa0f2821@googlegroups.com>
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

[toc] | [next] | [standalone]


#28584

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2012-09-06 08:16 -0400
Message-ID<mailman.297.1346933826.27098.python-list@python.org>
In reply to#28579
On Thu, Sep 6, 2012 at 8:00 AM, shaun <shaun.wiseman91@gmail.com> 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
> --
> 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

[toc] | [prev] | [next] | [standalone]


#28585

FromMRAB <python@mrabarnett.plus.com>
Date2012-09-06 13:20 +0100
Message-ID<mailman.298.1346934020.27098.python-list@python.org>
In reply to#28579
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

[toc] | [prev] | [next] | [standalone]


#28593

From"Colin J. Williams" <cjw@ncf.ca>
Date2012-09-06 09:41 -0400
Message-ID<k2a97q$3ek$1@theodyn.ncf.ca>
In reply to#28585
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.

[toc] | [prev] | [next] | [standalone]


#28592

FromDave Angel <d@davea.name>
Date2012-09-06 09:41 -0400
Message-ID<mailman.301.1346938934.27098.python-list@python.org>
In reply to#28579
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

[toc] | [prev] | [next] | [standalone]


#28643

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-09-06 17:23 -0400
Message-ID<mailman.330.1346966626.27098.python-list@python.org>
In reply to#28579
On Thu, 6 Sep 2012 05:00:32 -0700 (PDT), shaun
<shaun.wiseman91@gmail.com> 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
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web