Scope of Objects Passed as Arguments - Python
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 u开发者_如何学Gosing 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 passThank you Kirk ....
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.
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
.
精彩评论