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


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

Objects in Python

Started byshaun <shaun.wiseman91@gmail.com>
First post2012-08-22 07:13 -0700
Last post2012-08-22 11:29 -0600
Articles 6 on this page of 106 — 26 participants

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


Contents

  Objects in Python shaun <shaun.wiseman91@gmail.com> - 2012-08-22 07:13 -0700
    Re: Objects in Python Joel Goldstick <joel.goldstick@gmail.com> - 2012-08-22 10:31 -0400
    Re: Objects in Python Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-08-22 17:31 +0300
    Re: Objects in Python Peter Otten <__peter__@web.de> - 2012-08-22 16:36 +0200
    Re: Objects in Python lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-22 15:59 +0100
      Re: Objects in Python MRAB <python@mrabarnett.plus.com> - 2012-08-22 16:58 +0100
        Re: Objects in Python lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-22 17:10 +0100
          Re: Objects in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-22 17:30 +0100
            Re: Objects in Python lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-22 18:06 +0100
              Re: Objects in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-22 19:07 +0100
                Re: Objects in Python lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-22 20:13 +0100
      Re: Objects in Python Terry Reedy <tjreedy@udel.edu> - 2012-08-22 13:01 -0400
        Re: Objects in Python lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-22 18:46 +0100
          Re: Objects in Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-22 12:15 -0600
            Re: Objects in Python lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-22 20:03 +0100
              Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-23 12:02 +1000
                Re: Objects in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-23 04:11 +0000
                  Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-23 15:26 +1000
                  Re: Objects in Python Jan Kuiken <jan.kuiken@quicknet.nl> - 2012-08-23 20:02 +0200
                    Re: Objects in Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-23 12:17 -0600
                      Re: Objects in Python Jan Kuiken <jan.kuiken@quicknet.nl> - 2012-08-23 22:43 +0200
                    Re: Objects in Python 88888 Dihedral <dihedral88888@googlemail.com> - 2012-08-25 23:14 -0700
          Re: Objects in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-22 19:23 +0100
          Re: Re: Objects in Python Evan Driscoll <driscoll@cs.wisc.edu> - 2012-08-22 14:03 -0500
            Re: Objects in Python lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-22 20:45 +0100
              Re: Objects in Python MRAB <python@mrabarnett.plus.com> - 2012-08-22 21:31 +0100
              Re: Objects in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-22 21:46 +0100
                Methods versus functions [was Re: Objects in Python] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-23 04:07 +0000
              Re: Re: Objects in Python Evan Driscoll <driscoll@cs.wisc.edu> - 2012-08-22 16:31 -0500
                Re: Objects in Python lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-23 10:19 +0100
                  Re: Re: Objects in Python Evan Driscoll <driscoll@cs.wisc.edu> - 2012-08-23 11:44 -0500
                    Re: Objects in Python lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-23 18:56 +0100
          Re: Objects in Python Ben Finney <ben+python@benfinney.id.au> - 2012-08-23 09:58 +1000
            Re: Objects in Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-22 18:10 -0600
            Re: Re: Objects in Python Evan Driscoll <driscoll@cs.wisc.edu> - 2012-08-22 23:49 -0500
              Re: Objects in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-23 06:55 +0000
                Re: Objects in Python Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-08-23 11:59 +0300
                  Re: Objects in Python MRAB <python@mrabarnett.plus.com> - 2012-08-23 12:28 +0100
                  Re: Objects in Python Jerry Hill <malaclypse2@gmail.com> - 2012-08-23 10:43 -0400
                  Re: Objects in Python Evan Driscoll <driscoll@cs.wisc.edu> - 2012-08-23 12:17 -0500
                    Re: Objects in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-23 17:56 +0000
                      Variables vs names [was: Objects in Python] Evan Driscoll <driscoll@cs.wisc.edu> - 2012-08-23 14:22 -0500
                        Re: Variables vs names Ben Finney <ben+python@benfinney.id.au> - 2012-08-24 10:02 +1000
                        Re: Variables vs names [was: Objects in Python] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-25 02:05 +0000
                          Re: Variables vs names Ben Finney <ben+python@benfinney.id.au> - 2012-08-25 15:24 +1000
                      Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-24 08:00 +1000
                        Re: Objects in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-25 03:04 +0000
                          Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-25 16:34 +1000
                          Re: Objects in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-25 09:55 +0100
                          Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-25 20:23 +1000
                          Re: Objects in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-25 12:01 +0100
                          Re: Objects in Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-25 15:56 -0400
                          Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-26 09:27 +1000
                          Re: Objects in Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-25 20:43 -0400
                          Re: Re: Objects in Python Evan Driscoll <driscoll@cs.wisc.edu> - 2012-08-26 00:25 -0500
                      Re: Variables vs names [was: Objects in Python] Chris Angelico <rosuav@gmail.com> - 2012-08-24 09:34 +1000
                      Re: Objects in Python Ben Finney <ben+python@benfinney.id.au> - 2012-08-24 09:49 +1000
                      Re: Variables vs names [was: Objects in Python] Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-23 19:52 -0400
                      Re: Objects in Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-23 19:54 -0400
                      Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-24 10:01 +1000
                  Re: Objects in Python Terry Reedy <tjreedy@udel.edu> - 2012-08-23 13:17 -0400
              Re: Objects in Python Ben Finney <ben+python@benfinney.id.au> - 2012-08-24 00:16 +1000
              Re: Objects in Python Roy Smith <roy@panix.com> - 2012-08-23 20:36 -0400
                Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-24 11:34 +1000
                  Re: Objects in Python alex23 <wuwei23@gmail.com> - 2012-08-23 20:17 -0700
                    Re: Re: Objects in Python Evan Driscoll <driscoll@cs.wisc.edu> - 2012-08-24 04:14 -0500
                      Re: Objects in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-24 10:00 +0000
                        Re: Objects in Python Grant Edwards <invalid@invalid.invalid> - 2012-08-24 13:27 +0000
                          Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-25 05:18 +1000
                        Re: Re: Objects in Python Evan Driscoll <driscoll@cs.wisc.edu> - 2012-08-26 00:45 -0500
                          Re: Objects in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-26 13:43 +0000
                            Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-26 23:58 +1000
                              Re: Objects in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-26 14:18 +0000
                                Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-27 00:54 +1000
                                  Re: Objects in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-26 22:47 +0000
                            Re: Objects in Python Roy Smith <roy@panix.com> - 2012-08-26 10:02 -0400
                              Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-27 00:14 +1000
                            Re: Objects in Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-26 16:12 -0400
                              Re: Objects in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-26 23:29 +0000
                        Re: Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-26 16:22 +1000
                          Re: Objects in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-26 12:02 +0000
                            Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-26 23:34 +1000
                            Re: Objects in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-26 15:02 +0100
                            Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-27 00:05 +1000
                          Re: Objects in Python Roy Smith <roy@panix.com> - 2012-08-26 09:41 -0400
                Identity function id() [was Re: Objects in Python] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-24 10:06 +0000
            Re: Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-23 15:33 +1000
            Re: Objects in Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-23 14:30 -0400
              Re: Objects in Python Alexander Blinne <news@blinne.net> - 2012-08-24 15:23 +0200
            Re: Objects in Python Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2012-08-24 09:38 +0200
            Re: Objects in Python Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2012-08-24 10:03 +0200
          Re: Objects in Python Walter Hurry <walterhurry@lavabit.com> - 2012-08-23 01:19 +0000
            Re: Objects in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-23 04:14 +0000
              Re: Objects in Python lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-23 09:10 +0100
                Re: Objects in Python Ben Finney <ben+python@benfinney.id.au> - 2012-08-23 23:59 +1000
                  Re: Objects in Python lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-23 15:20 +0100
                    Re: Objects in Python Ben Finney <ben+python@benfinney.id.au> - 2012-08-24 00:24 +1000
            Re: Objects in Python lipska the kat <lipskathekat@yahoo.co.uk> - 2012-08-23 09:03 +0100
          Re: Objects in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-23 04:34 +0000
            Re: Objects in Python rusi <rustompmody@gmail.com> - 2012-08-23 10:04 -0700
    Re: Objects in Python John Gordon <gordon@panix.com> - 2012-08-22 15:03 +0000
    Re: Objects in Python shaun <shaun.wiseman91@gmail.com> - 2012-08-22 08:25 -0700
      Re: Objects in Python Chris Angelico <rosuav@gmail.com> - 2012-08-23 01:47 +1000
      Re: Objects in Python Dave Angel <d@davea.name> - 2012-08-22 11:51 -0400
      Re: Objects in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-22 17:13 +0100
      Re: Objects in Python Ian Kelly <ian.g.kelly@gmail.com> - 2012-08-22 11:29 -0600

Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]


#27643

FromJohn Gordon <gordon@panix.com>
Date2012-08-22 15:03 +0000
Message-ID<k12scn$n6t$1@reader1.panix.com>
In reply to#27634
In <18409992-1e28-4721-8e64-60c69668da4e@googlegroups.com> shaun <shaun.wiseman91@gmail.com> writes:

> I'm having an issue its my first time using python and i set up a class one of the methods is supposed to return a string but instead returns:

> <bound method Param.returnString of <Param.Param instance at 0x00C
> 389E0>>

It looks like you're referencing the method object itself, instead of
calling it method.  In other words, you've left off the parentheses.

I.e. you're doing something like this:

    print my_object.foo

Instead of this:

    print my_object.foo()

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

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


#27645

Fromshaun <shaun.wiseman91@gmail.com>
Date2012-08-22 08:25 -0700
Message-ID<12ee0624-f523-4cf9-b46c-292315ef789e@googlegroups.com>
In reply to#27634
Here is some code: 
//////////////////////////This is the object I want to create:
#!/usr/bin/python
import cx_Oracle
import sys
import time
import datetime


class batchParam:

	def __init__(self,array):
		self.array=array


	def breakuparray(self):
		for row in self.array:
			mer = row[0].ljust(25, ' ')
			merc = row[1].ljust(13, ' ')
			mertype = row[2]
			merloc = row[3]
			mercount = row[4]
			mersec = row[5]
			acq = row[6]
		
	
	
	def returnBatch(self):
		self.breakuparray()
		return "\x01001\x0251.%s%s%s%s%s%s%s%s\x03"  % (mer, merc, mertype, merloc, mercount, mersec, acq);


//////////////////////////////////////Here is the script I want to run the object in:


#!/usr/bin/python
import cx_Oracle
import sys
import time
import datetime
sys.path.append("C:\\Documents and Settings\\swiseman\\Desktop")
from batchParam import batchParam

term = sys.argv[1]
batch = sys.argv[2]

con = cx_Oracle.connect('databaseInfo')


cur = con.cursor()
cur.execute("SELECT * FROM SOME_TABLE))

results = cur.fetchall()

batchParam(results)
Batch=batchParam.returnBatch

print Batch

cur.close()

//////////////////////////////////////

Thanks,
Shaun

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


#27647

FromChris Angelico <rosuav@gmail.com>
Date2012-08-23 01:47 +1000
Message-ID<mailman.3659.1345650469.4697.python-list@python.org>
In reply to#27645
On Thu, Aug 23, 2012 at 1:25 AM, shaun <shaun.wiseman91@gmail.com> wrote:
>         def breakuparray(self):
>                 for row in self.array:
>                         mer = row[0].ljust(25, ' ')
>                         merc = row[1].ljust(13, ' ')
>                         mertype = row[2]
>                         merloc = row[3]
>                         mercount = row[4]
>                         mersec = row[5]
>                         acq = row[6]

The "for ... in ..." construct is a loop. I'm not sure what you're
trying to accomplish here, but you're taking the last entry in
self.array and unpacking that as a nested array. Perhaps not what you
had in mind.

For what you're doing there, though, a class is overkill. Remember,
Python isn't Java; the most natural way to do everything isn't
necessarily to write a class that unpacks things and packs them up
again in a different way.

ChrisA

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


#27648

FromDave Angel <d@davea.name>
Date2012-08-22 11:51 -0400
Message-ID<mailman.3660.1345650695.4697.python-list@python.org>
In reply to#27645
On 08/22/2012 11:25 AM, shaun wrote:
> Here is some code: 
> //////////////////////////This is the object I want to create:
> #!/usr/bin/python
> import cx_Oracle
> import sys
> import time
> import datetime
>
>
> class batchParam:
>
> 	def __init__(self,array):
> 		self.array=array
>
>
> 	def breakuparray(self):
> 		for row in self.array:
> 			mer = row[0].ljust(25, ' ')
> 			merc = row[1].ljust(13, ' ')
> 			mertype = row[2]
> 			merloc = row[3]
> 			mercount = row[4]
> 			mersec = row[5]
> 			acq = row[6]
> 		
> 	
> 	
> 	def returnBatch(self):
> 		self.breakuparray()
> 		return "\x01001\x0251.%s%s%s%s%s%s%s%s\x03"  % (mer, merc, mertype, merloc, mercount, mersec, acq);
>
>
> //////////////////////////////////////Here is the script I want to run the object in:
>
>
> #!/usr/bin/python
> import cx_Oracle
> import sys
> import time
> import datetime
> sys.path.append("C:\\Documents and Settings\\swiseman\\Desktop")
> from batchParam import batchParam
>
> term = sys.argv[1]
> batch = sys.argv[2]
>
> con = cx_Oracle.connect('databaseInfo')
>
>
> cur = con.cursor()
> cur.execute("SELECT * FROM SOME_TABLE))
>
> results = cur.fetchall()
>
> batchParam(results)

This creates an instance of batchParam, but doesn't save it anywhere. 
So it's discarded immediately.

> Batch=batchParam.returnBatch
This tries to returns a reference to a static method of the class. 
Without an object, you won't get access to normal instance methods;
there's no 'self'.  And without parentheses, you won't even try to call
the method, right or wrong.

Probably you wanted something like:

    obj = batchParam(results)   #now obj is an instance
    mystring = obj.returnBatch()    #calls the method, and saves the
returned string
    print mystring

>
> print Batch
>
> cur.close()
>
>

Other comments:   Don't make the mistake of forcing every class into its
own source file.  Unlike java, python has no such restrictions.  It also
has ordinary functions, not part of any class.   So if several classes
are related, go ahead and put them in a common file.  Or keep them
separate, Python doesn't mind.

There are capitalization conventions:  class names start with a capital
letter, and source code filenames do not.  So the class you've got in 
batchParam could be called BatchParam.

Neither of these matter much, but they make it easier for someone else
to see what you were trying to do.

It would also be helpful if you posted the complete error message (with
traceback), so we could more easily guess where in the code the problem
occurs.  It can be useful to add a comment in the actual source you
post, since you have line numbers in your editor, and we don't in our
emails.  But don't try to get cute with colors, as this is a text
forum.  (that last comment may not apply to you, since you already used
a plain-text format for your message)

Python does have classmethod and staticmethod, but that's not usually
what you want, and not here.


-- 

DaveA

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


#27651

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-08-22 17:13 +0100
Message-ID<mailman.3662.1345651903.4697.python-list@python.org>
In reply to#27645
On 22/08/2012 16:47, Chris Angelico wrote:
>
> For what you're doing there, though, a class is overkill. Remember,
> Python isn't Java; the most natural way to do everything isn't
> necessarily to write a class that unpacks things and packs them up
> again in a different way.
>
> ChrisA
>

This shows just how poor the Python documentation is.  I can't find the 
"overcoming brainwashing" section anywhere!!!

-- 
Cheers.

Mark Lawrence.

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


#27661

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-08-22 11:29 -0600
Message-ID<mailman.3670.1345656590.4697.python-list@python.org>
In reply to#27645
In addition to the excellent feedback that Dave gave you:

On Wed, Aug 22, 2012 at 9:25 AM, shaun <shaun.wiseman91@gmail.com> wrote:
>         def breakuparray(self):
>                 for row in self.array:
>                         mer = row[0].ljust(25, ' ')
>                         merc = row[1].ljust(13, ' ')
>                         mertype = row[2]
>                         merloc = row[3]
>                         mercount = row[4]
>                         mersec = row[5]
>                         acq = row[6]

This loops over each row in self.array and stores the contents in a
set of variables called mer, merc, etc.  Note that as written these
variables are *local* to the breakuparray method, not attributes of
the class.  Also note that on each iteration, you reassign to the same
variables again, wiping out all the work you did on the previous
iteration.  In the end, only the last row is stored in the local
variables, and then even that is wiped out when the method returns.

>         def returnBatch(self):
>                 self.breakuparray()
>                 return "\x01001\x0251.%s%s%s%s%s%s%s%s\x03"  % (mer, merc, mertype, merloc, mercount, mersec, acq);

This appears to be trying to use the same local variables from the
breakuparray method, but it can't.  Those are out of scope here.  If
you want to share these data between the breakuparray method and the
returnBatch method, you should either have breakuparray return them,
or you should store them as attributes on the object instance:
self.mer, self.merc, self.mertype, etc. (In Python, object attribute
storage is always explicit, never implicit as in Java and the rest of
the C++ family).

[toc] | [prev] | [standalone]


Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]

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


csiph-web