Discussion:
Passing information from one function to another in a large application
Paul Thompson
2014-09-07 22:36:04 UTC
Permalink
I am working with wxPython and Python in general to design an image
processing tool. I am a little confused about one thing with Python -
passing information. I am tempted to set a lot of items as global, but that
is considered less than optimal by some. So, do I pass them through the
call as parameters, and get them back as return values? Or have I missed
something?
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Michael Moriarity
2014-09-07 23:34:57 UTC
Permalink
Post by Paul Thompson
I am working with wxPython and Python in general to design an image
processing tool. I am a little confused about one thing with Python -
passing information. I am tempted to set a lot of items as global, but that
is considered less than optimal by some. So, do I pass them through the
call as parameters, and get them back as return values? Or have I missed
something?
--
You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
This is a pretty general question, and I'm sure I don't have the best
answer. However, in my python applications, with or without wx, I tend to
organize the persistent data as attributes of objects. Most times, it is
fairly obvious which object should have contain a particular data item.
Then, when it is necessary to use the data item in some other part of the
program, I will pass either the item itself, or a reference to the object
which contains it as an argument to the function which requires it.

I also use globals more than most people recommend, by creating a namespace
specifically for them. I will have a file called Globals.py, which will
contain only data and functions that I have decided need to be available
everywhere. Wherever I need to use such data I will "import Globals as G"
and then refer to "G.MyDataItem". Hope this helps.
--
Best Regards,
Michael Moriarity
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Rufus Smith
2014-09-08 16:02:10 UTC
Permalink
Post by Paul Thompson
I am working with wxPython and Python in general to design an image
processing tool. I am a little confused about one thing with Python -
passing information. I am tempted to set a lot of items as global, but
that is considered less than optimal by some. So, do I pass them
through the call as parameters, and get them back as return values? Or
have I missed something?
--
You received this message because you are subscribed to the Google
Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
When I started with Python, I used globals. Not for long. It is too
easy to forget the global statement at the start of functions
that modify the global object, which leads to obscure errors, because
the function is happy to create the new, non-global
object, which, of course, disappears at the function's return.

Better to have a global dictionary, or multiple global dictionaries and
access the individual objects with keys.

I've dabbled with web2py and they have a great class called Storage in
their gluon module which is really convenient for this
sort of thing: You can access your dictionary items with keys that look
like attribute names. Also, the default return for undefined keys
is "None", rather than raising an AttributeError. Which is often more
convenient.

So, in an application such as yours, you might have a global User object
containing:

User.Preferences.*
User.Images
User.Environment.*

etc.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nathan McCorkle
2014-09-08 16:49:54 UTC
Permalink
Post by Paul Thompson
I am working with wxPython and Python in general to design an image
processing tool. I am a little confused about one thing with Python -
passing information. I am tempted to set a lot of items as global, but that
is considered less than optimal by some. So, do I pass them through the
call as parameters, and get them back as return values? Or have I missed
something?
For some things the OS ClipBoard makes sense.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Tim Roberts
2014-09-08 17:26:19 UTC
Permalink
Post by Paul Thompson
I am working with wxPython and Python in general to design an
image processing tool. I am a little confused about one thing with
Python - passing information. I am tempted to set a lot of items
as global, but that is considered less than optimal by some. So,
do I pass them through the call as parameters, and get them back
as return values? Or have I missed something?
For some things the OS ClipBoard makes sense.
No. If the clipboard is the answer, then you have asked the wrong
question. There is NEVER a case where the clipboard is an acceptable
alternative for passing data between Python objects.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nathan McCorkle
2014-09-08 17:50:38 UTC
Permalink
Post by Nathan McCorkle
Post by Paul Thompson
I am working with wxPython and Python in general to design an image
processing tool. I am a little confused about one thing with Python -
passing information. I am tempted to set a lot of items as global, but that
is considered less than optimal by some. So, do I pass them through the
call as parameters, and get them back as return values? Or have I missed
something?
For some things the OS ClipBoard makes sense.
No. If the clipboard is the answer, then you have asked the wrong
question. There is NEVER a case where the clipboard is an acceptable
alternative for passing data between Python objects.
The OP said between applications, not Python objects.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Marc Tompkins
2014-09-08 17:54:33 UTC
Permalink
Post by Nathan McCorkle
The OP said between applications, not Python objects.
Where did he say that?
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nathan McCorkle
2014-09-08 18:08:03 UTC
Permalink
Post by Marc Tompkins
Post by Nathan McCorkle
The OP said between applications, not Python objects.
Where did he say that?
The title of the post 'Passing information from one function to another in
a large application'
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nathan McCorkle
2014-09-08 18:13:30 UTC
Permalink
Post by Nathan McCorkle
Post by Marc Tompkins
Post by Nathan McCorkle
The OP said between applications, not Python objects.
Where did he say that?
The title of the post 'Passing information from one function to another
in a large application'
Oh, I guess I mis-read between-applications
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Mike Driscoll
2014-09-08 18:04:33 UTC
Permalink
Post by Nathan McCorkle
Post by Nathan McCorkle
Post by Paul Thompson
I am working with wxPython and Python in general to design an image
processing tool. I am a little confused about one thing with Python -
passing information. I am tempted to set a lot of items as global, but that
is considered less than optimal by some. So, do I pass them through the
call as parameters, and get them back as return values? Or have I missed
something?
For some things the OS ClipBoard makes sense.
No. If the clipboard is the answer, then you have asked the wrong
question. There is NEVER a case where the clipboard is an acceptable
alternative for passing data between Python objects.
The OP said between applications, not Python objects.
The OP is talking about passing information around either via parameters or
by creating globals. He also mentions needing the return values. I don't
see where the clipboard comes into this.

- Mike
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Steve Barnes
2014-09-08 17:54:01 UTC
Permalink
Post by Paul Thompson
I am working with wxPython and Python in general to design an image
processing tool. I am a little confused about one thing with Python -
passing information. I am tempted to set a lot of items as global, but
that is considered less than optimal by some. So, do I pass them
through the call as parameters, and get them back as return values? Or
have I missed something?
--
You received this message because you are subscribed to the Google
Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
It is usually better to store things in class instances and then either
call the class methods or pass the instance to the functions - bear in
mind that python uses references by default so this is efficient - but
for more complex applications there is a strong case for using the
PubSub <http://pubsub.sourceforge.net/> model as you are less likely to
see things like GUI hangs.

An old rule of thumb for maintainable code that I was given, many years
ago, was if a program totalled more than 100 lines of code then it
should have already gotten rid of global variables - I am not sure if
everybody would agree but certainly if your code is likely to be in the
1000s of lines then global variables, (possibly for anything other than
start-up flags), then global variables are likely to be a pain.

Gadget/Steve
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Mike Driscoll
2014-09-08 18:09:36 UTC
Permalink
Hi,
Post by Paul Thompson
I am working with wxPython and Python in general to design an image
processing tool. I am a little confused about one thing with Python -
passing information. I am tempted to set a lot of items as global, but that
is considered less than optimal by some. So, do I pass them through the
call as parameters, and get them back as return values? Or have I missed
something?
I would recommend against using globals. Here's one good explanation why:
http://stackoverflow.com/questions/19158339/python-why-are-global-variables-evil

If you are following Object Oriented design (which you should if you're
using wxPython), then most of your data will be in classes. This means that
you usually don't have to pass much of anything around as you can just turn
some of the local variables into class attributes. For example, if you have
a variable called "x", you can turn it into a class variable by prepending
it with "self" so that it becomes "self.x". This allows you to access this
value and change it anywhere within your class. If you need to pass
information between classes, then you can do that via the parameters you
mentioned or via pubsub. I prefer the latter as I find it to be a cleaner
solution.

Hope that helps!
Mike
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...