作为参数传递的对象的范围 - Python

发布于 2024-11-14 06:49:15 字数 2463 浏览 2 评论 0原文

我想将 sqlalchemy 的会话对象发送到另一个类的函数。

从类OraDialog的函数oraconnect到类oraconnassign 功能。

实际上我的需要是在oraconn的另一个函数map中使用session对象

我无法将session对象直接发送到 map 函数,因为它是一个槽 - 它在 之前触发。 我打算定义分配函数主要是为了获取会话对象。

错误:(包括打印语句)

<0x030808F0处的sqlalchemy.orm.session.Session对象>
这是在分配中

这是在地图上
回溯(最近一次调用最后一次):
文件“C:\Users\Arul\Desktop\dbvis\oraconn.py”,第 44 行,在地图中
for t in self.s.query(tables):
AttributeError:'NoneType'对象没有属性'query'

oraconn类的分配和映射函数:

def assign(self,s):
    self.s=s
    print self.s
    print "This is in assign"
def map(self):
    print self.s
    print "This is in map"
    self.table_no=0
    for t in self.s.query(tables):
        self.table_no=self.table_no+1
        table_list.append(t.table_name)
    self.item=QtCore.QStringList()
    for c in self.s.query(columns):
        self.item.append(c.column_name)
    self.ui.list_col.addItems(self.item)

SO, 分配函数正确接收对象。但地图功能无法使用它 - 它说 Nonetype

我的问题: 同一类的两个函数不能使用一个对象?

我一定做了一些愚蠢的事情 - 所以请指出......

(我正在使用 windows7 / python 2.6 / PyQt4 /sqlalchemy /cx_oracle)

编辑:
调用类:

class OraDialog(QtGui.QDialog):
def __init__(self,parent=None):
    QtGui.QDialog.__init__(self,parent)
    self.odia=Ui_Dialog()
    self.odia.setupUi(self)
    self.uiobj=oraconn()
    QtCore.QObject.connect(self.odia.but_con,QtCore.SIGNAL('clicked()'),self.uiobj.oraconnect)
def oraconnect(self):
    self.setVisible(0)
    eng_str="oracle://"+self.odia.line_user.text()+":"+self.odia.line_pass.text()+"@localhost:1521/"+self.odia.line_sid.text()+"?mode="+self.odia.line_role.text()
    engine_str=str(eng_str)
    engine=create_engine(engine_str)
    Session=sessionmaker(engine)
    self.s=Session()
    print self.s

问题解决:
问题是 - 我创建了两个实例并从一个实例传递到另一个实例。

更正的代码:

oraconn.assign(myapp,self.s)

其中
oraconn->类
分配-> oraconn的功能
我的应用程序 -> oraconn 的实例,为主应用程序声明
self.s->我想通过的论点

谢谢柯克....

I want to send a session object of sqlalchemy to a function of another class .

From class OraDialog's function oraconnect to class oraconn's assign function .

Actually My need is to use the session object in oraconn's another function map

I can't send the session object directly to map function because it is a slot - it is triggered before .
I intended to define the assign function primarily to get the session object.

The Error : ( including print statements )

<sqlalchemy.orm.session.Session object at 0x030808F0>
<sqlalchemy.orm.session.Session object at 0x030808F0>
This is in assign
None
This is in map
Traceback (most recent call last):
File "C:\Users\Arul\Desktop\dbvis\oraconn.py", line 44, in map
for t in self.s.query(tables):
AttributeError: 'NoneType' object has no attribute 'query'

The oraconn class's assign and map functions :

def assign(self,s):
    self.s=s
    print self.s
    print "This is in assign"
def map(self):
    print self.s
    print "This is in map"
    self.table_no=0
    for t in self.s.query(tables):
        self.table_no=self.table_no+1
        table_list.append(t.table_name)
    self.item=QtCore.QStringList()
    for c in self.s.query(columns):
        self.item.append(c.column_name)
    self.ui.list_col.addItems(self.item)

SO ,
The assign function receives the object properly . But the map function can't use it - It says Nonetype

My Question :
Two functions of same class can't use an object ?

I must have done something silly - so plz point out......

( I'm using windows7 / python 2.6 / PyQt4 /sqlalchemy /cx_oracle)

Edit :
The calling class :

class OraDialog(QtGui.QDialog):
def __init__(self,parent=None):
    QtGui.QDialog.__init__(self,parent)
    self.odia=Ui_Dialog()
    self.odia.setupUi(self)
    self.uiobj=oraconn()
    QtCore.QObject.connect(self.odia.but_con,QtCore.SIGNAL('clicked()'),self.uiobj.oraconnect)
def oraconnect(self):
    self.setVisible(0)
    eng_str="oracle://"+self.odia.line_user.text()+":"+self.odia.line_pass.text()+"@localhost:1521/"+self.odia.line_sid.text()+"?mode="+self.odia.line_role.text()
    engine_str=str(eng_str)
    engine=create_engine(engine_str)
    Session=sessionmaker(engine)
    self.s=Session()
    print self.s

Problem Solved :
The problem is - i've created two instances and passed from one to another .

Corrected code :

oraconn.assign(myapp,self.s)

where
oraconn -> class
assign -> function of oraconn
myapp -> instance of oraconn , declared for main application
self.s -> the argument i wanted to pass

Thank you Kirk ....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2024-11-21 06:49:15

如果您使用 Qt,那么这可能是一个 GUI 应用程序。也许存在线程问题;也许self指的是不同的对象。仅凭您给我们的信息很难判断。在继续之前,请

print self

在每个方法的顶部附近添加:。至少这样你可以确认两者是否在同一个对象上操作。


在 OraDialog.init 中,您说的是 self.uiobj=oraconn()。这是给你带来问题的物体吗?如果是这样:请注意,您正在将 at 创建为实例变量,以便每个 OraDialog 对象都有一个不同的 .uiobj 对象。您可以尝试通过编写以下内容来使其成为类变量吗

class OraDialog(QtGui.QDialog):
    uiobj = oraconn()
    def __init__() [...]

?也就是说,将 uiobj 定义为属于该类的所有实例,而不仅仅是一个实例。

If you're using Qt, then this is likely a GUI app. Maybe there are threading issues; maybe self refers to different objects. It's hard to tell with only what you've given us. Before you go any further, add:

print self

near the top of each method. At least then you can confirm whether both are operating on the same object.


In OraDialog.init, you're saying self.uiobj=oraconn(). Is that the object that's giving you problems? If so: notice that you're creating at as an instance variable, so that each OraDialog object has a different .uiobj object. Could you try making that a class variable by writing:

class OraDialog(QtGui.QDialog):
    uiobj = oraconn()
    def __init__() [...]

instead? That is, define uiobj as belonging to all instances of the class, not just one instance.

带上头具痛哭 2024-11-21 06:49:15

您发布的代码似乎没问题,我看不出任何明显的错误。

问题一定出在调用方。由于自引用可以显式传递给方法(通过 OraDialog.map(my_object),其中 my_object 可以是任何内容),这完全有可能。另请注意,这与范围界定无关。

您应该检查 map 函数的调用代码。它很可能在不同的对象上被调用为assign

The code you have posted seems to be okay, I can't spot any obvious mistakes.

The problem must be on the calling side. As the self reference can be passed explicitly to methods (via OraDialog.map(my_object), where my_object could be anything) this perfectly possible. Also note that this has nothing to do with scoping.

You should check calling code of the map function. It is very likely it is called on a different object as assign.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文