【问题标题】:Class instance in PythonPython中的类实例
【发布时间】:2012-03-13 22:51:42
【问题描述】:

我在 Python 中使用类实例时遇到问题。 我创建了一个从 cx_Oracle 包继承连接类的新类 ora。 当我尝试运行此代码时,我会收到信息

文件“pyt.py”,第 12 行,在 myquery 中 ora.myConnect.cursor() AttributeError: 'NoneType' 对象没有属性 'cursor'

因此 Python 无法识别在 ora.myConnect 中存储了对实例的引用。 我不知道t know what can be reason of this error and what its 的代码有误。

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()

编辑

ve tried to replace ora to self but still Python dont 可以访问实例

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()   

错误: self.myConnect.cursor() AttributeError: 'NoneType' 对象没有属性 'cursor'

EDIT2 这段代码在没有 OOP 的情况下工作,对我来说 self.myConnect 应该引用对象实例,并且这个对象应该包含方法 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()

【问题讨论】:

  • self.myConnect = connect.__init__(self,connstr) 很奇怪。 __init__ 方法似乎不太可能返回游标。您确定您了解您要扩展的类应该如何工作吗?
  • 基于the documentation here 我会说你不应该像你所做的那样扩展connect。相反,只需从您的__init__ 调用cx_Oracle.connect() 并将连接保存为self.myConnect
  • self.myConnect 应该返回对对象实例的引用,例如,如果没有 OOP,此代码可以工作 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()
  • 我在下面更新了我的答案。
  • 谢谢@beerbajay 你的方法没有继承也很好,感谢帮助

标签: python oop class


【解决方案1】:

好像你想要self:

class ora(connect):
    myConnect = None

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

    # ...

ora 是类的名称,而不是实例的名称。

更新尝试以下操作:

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()   

【讨论】:

  • 如果在__init__myquery 中将ora 替换为self,您的connection 对象应该可以正常工作。你遇到了什么错误?
  • 我遇到了 cmets fromating 的问题,所以我在第一篇文章中添加了更多细节
【解决方案2】:

为什么你想让self.myConnect 引用connect 实例?这完全是对 OOP 的误解。 ora 实例 connect 实例。 self.cursor 是您找到光标的位置。

您的代码应如下所示:

class ora(connect):

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

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

在任何情况下,__init__ 绝对不能返回任何内容,因此将self.myConnect 设置为返回值将始终导致它绑定到None

【讨论】:

  • 谢谢 - 我不知道这个方法超级,现在效果很好
猜你喜欢
  • 2013-06-22
  • 2013-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
相关资源
最近更新 更多