Procedural API inside class--scoping questions

K

Kevin Walzer

I'm trying to make use of a Python library, aemreceive, that provides a
procedural API. (aemreceive is a library for Python on the Mac that
allows the application to receive and respond to Apple Events.)

My Python apps basically run in a single fooApp class, and everything
runs inside the class. To launch the app (my apps are Tkinter-based), I
use something like this:

if __name__== '__main__':
app = fooApp(None)
app.mainloop()

In keeping with aemreceive's procedural API, I've added a runCommand
function inside the app class to provide some basic functionality in
response to Apple Events it will set some objects/variables, then
display the output in the app. In my code it looks like this:

def runCommand(string):
self.searchterm=string
self.servertree.selection_set('Default')
self.getInfo()

When I test this command, I get an error from Python: "self" is not
defined.

I think I understand what is going here. All of the other functions in
the fooApp class take "self" as a parameter, i.e. they are components of
the class. runCommand is not, so therefore "self" is undefined. My
question is, how can I get the values from the class (self.searchterm,
et.al) inside the runCommand function?

Thanks,
Kevin
 
A

Arnaud Delobelle

I'm trying to make use of a Python library, aemreceive, that provides a
procedural API. (aemreceive is a library for Python on the Mac that
allows the application to receive and respond to Apple Events.)

My Python apps basically run in a single fooApp class, and everything
runs inside the class. To launch the app (my apps are Tkinter-based), I
use something like this:

if __name__== '__main__':
     app = fooApp(None)
     app.mainloop()

In keeping with aemreceive's procedural API, I've added a runCommand
function inside the app class to provide some basic functionality in
response to Apple Events it will set some objects/variables, then
display the output in the app. In my code it looks like this:

     def runCommand(string):
         self.searchterm=string
         self.servertree.selection_set('Default')
         self.getInfo()

When I test this command, I get an error from Python: "self" is not
defined.

Is runcommand is method of your class? In that case you should define
it as:

def runCommand(self, string):
...

HTH
 
B

Bruno Desthuilliers

Kevin Walzer a écrit :
I'm trying to make use of a Python library, aemreceive, that provides a
procedural API. (aemreceive is a library for Python on the Mac that
allows the application to receive and respond to Apple Events.)

My Python apps basically run in a single fooApp class, and everything
runs inside the class. To launch the app (my apps are Tkinter-based), I
use something like this:

if __name__== '__main__':
app = fooApp(None)
app.mainloop()

In keeping with aemreceive's procedural API, I've added a runCommand
function inside the app class to provide some basic functionality in
response to Apple Events it will set some objects/variables, then
display the output in the app. In my code it looks like this:

def runCommand(string):
self.searchterm=string
self.servertree.selection_set('Default')
self.getInfo()

When I test this command, I get an error from Python: "self" is not
defined.
Indeed.

I think I understand what is going here. All of the other functions in
the fooApp class take "self" as a parameter, i.e. they are components of
the class.

It actually works the other way round : it's because they are
"components" (we prefer to say "attributes") of the class that they take
the current instance as first param. FWIW, the following syntaxes are
functionnaly equivalent:

obj = MyClass()
obj.some_method()
MyClass.some_method(obj)
# if some_method is not inherited:
MyClass.__dict__['some_method'](obj)


Anyway:
runCommand is not, so therefore "self" is undefined. My
question is, how can I get the values from the class (self.searchterm,

s/class/instance/ here.

et.al) inside the runCommand function?

The usual way - adds the relevant param:

def runCommand(self, string):
self.searchterm=string
self.servertree.selection_set('Default')
self.getInfo()


The problem is that you don't provide much information about how this
function is actually called.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top