Objects in Python

Dave Angel d at davea.name
Wed Aug 22 11:51:05 EDT 2012


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




More information about the Python-list mailing list