browarq
browarq

Reputation: 3

Class instance in Python

I have problem with using class instance in Python. Ive created a new class ora which inherit connect class from cx_Oracle package. When I try tu run this code I recive information

File "pyt.py", line 12, in myquery ora.myConnect.cursor() AttributeError: 'NoneType' object has no attribute 'cursor'

So Python cannote recognize that in ora.myConnect is stored reference to instance. I dont know what can be reason of this error and what its wrong with code.

from cx_Oracle import connect

class ora(connect):
  myConnect = None

  def __init__(self,connstr):    
    ora.myConnect = connect.__init__(self,connstr)


  def myquery(self):
      ora.myConnect.cursor()
      ora.myConnect.cursor.execute("SELECT * FROM table")
      ora.myConnect.cursor.close()   



connstr = 'user/passwd@host:port/sid' 
connection = ora(connstr)      
connection.myquery()                 
connection.close()

EDIT

Ive tried to replace ora to self but still Python dont have access to instance

from cx_Oracle import connect

class ora(connect):
  myConnect = None

  def __init__(self,connstr):    
    self.myConnect = connect.__init__(self,connstr)
  def myquery(self):
      self.myConnect.cursor()
      self.myConnect.cursor.execute("SELECT * FROM table")
      self.myConnect.cursor.close()   

Error: self.myConnect.cursor() AttributeError: 'NoneType' object has no attribute 'cursor'

EDIT2 This code works without OOP, for me self.myConnect sholud reference to object instance and this object should contain method cursor()

import cx_oracle
connstr = 'user/passwd@host:port/sid' 
connection = cx_oracle.connect(connstr)                   
cursor = connection.cursor()                              
cursor.execute("SELECT * FROM table")
cursor.close()
connection.close()

Upvotes: 0

Views: 1165

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599470

Why do you want self.myConnect to refer to the connect instance? That's a complete misunderstanding of OOP. The ora instance is the connect instance. self.cursor is where you find the cursor.

Here's how your code should look:

class ora(connect):

  def __init__(self,connstr):    
    super(ora, self).__init__(connstr)

  def myquery(self):
    self.cursor.execute("SELECT * FROM table")
    self.cursor.close()   

In any case, __init__ must never return anything, so setting self.myConnect to the return value will always result in it being bound to None.

Upvotes: 1

beerbajay
beerbajay

Reputation: 20270

It seems like you want self:

class ora(connect):
    myConnect = None

    def __init__(self, connstr):    
        self.myConnect = connect.__init__(self, connstr)

    # ...

ora is the name of the class, not the instance.

Update Try the following:

from cx_Oracle import connect

class ora:
    myConnect = None

    def __init__(self, connstr):    
        self.myConnect = connect(connstr)

    def myquery(self):
        self.myConnect.cursor()
        self.myConnect.cursor.execute("SELECT * FROM table")
        self.myConnect.cursor.close()   

Upvotes: 2

Related Questions