The Artima Developer Community
Sponsored Link

Python Answers Forum
Problem writing a class

4 replies on 1 page. Most recent reply: Nov 11, 2004 4:28 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Maverick

Posts: 11
Nickname: maverick43
Registered: Oct, 2004

Problem writing a class Posted: Nov 5, 2004 8:15 AM
Reply to this message Reply
Advertisement
Hi all,
I am trying to write a class that takes in uname,pwd and dsn info (using cx_Oracle DBI) and connects to db.

here is the sameple code



------baseCursor.py-------------
Import cx_Oracle

class baseCursor:
def __init__(self,uname,pwd,dsn):
self.conn=cx_Oracle.connect(unmae,pwd,dsn)
self.surcor=conn.cursor()
-------------------------------------


---test.py--- -----------
import baseCursor

uname='uname1'
pwd='pwd1'
dsn='oracle_db'
tescur=baseCursor(uname,p wd,dsn)

-----------------------------------

And i'm getting the following message :
Traceback (most recent call last):
File "C:\Sree\Python\test.py", line 6, in -toplevel-
testcur=baseCursor1(uname,pwd,dsn)
TypeError: 'module' object is not callable

I am trying to learn how to write classes and got stuck with this one..
Any ideas/Suggestions?
Thanks a lot..


Erin

Posts: 1
Nickname: derkin
Registered: Oct, 2004

Re: Problem writing a class Posted: Nov 5, 2004 8:42 AM
Reply to this message Reply
Had the same problem. Change the class name then call create the class like so tClass = baseCursor.<newClassName>(uname,p wd,dsn)

Hope this helps.

Andy

Posts: 28
Nickname: adn
Registered: Jul, 2004

Re: Problem writing a class Posted: Nov 8, 2004 7:07 AM
Reply to this message Reply
It's because your python file is called the same as the class you're after. In your test.py file, you're getting the right module but calling the module rather than the class within.

tescur = baseCursor(uname, pwd, dsn)

Should be;

tescur = baseCursor.baseCursor(uname, pwd, dsn)

Maverick

Posts: 11
Nickname: maverick43
Registered: Oct, 2004

Re: Problem writing a class Posted: Nov 9, 2004 12:50 PM
Reply to this message Reply
Sorry, I was little late checking my threads. I think you guys are right. I was not using the class name within. I also changed the class name to some other name. It works now.

Thanks for your help.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Problem writing a class Posted: Nov 11, 2004 4:28 PM
Reply to this message Reply
There's nothing wrong with the class having the same name as the module. There are several cases in the Python library where the main class or main function of a module has the same name as the module.

Also, if you don't like the redundancy on the usage side then you can use the "from" style import:
from baseCursor import baseCursor
 
testCursor = baseCursor( ... )

Flat View: This topic has 4 replies on 1 page
Topic: Drawing on an image widget Previous Topic   Next Topic Topic: Need to run Python script on Unix box with no python S/W

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use