Passing information from one function to another in a large application

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?

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.

···

On Sun, Sep 7, 2014 at 6:36 PM, Paul Thompson <patjahsd@gmail.com> wrote:

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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
Best Regards,
Michael Moriarity

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.

···

On 9/7/2014 6:36 PM, Paul Thompson
wrote:

    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+unsubscribe@googlegroups.com.

  For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

For some things the OS ClipBoard makes sense.

···

On Sunday, September 7, 2014 3:36:04 PM UTC-7, Paul Thompson wrote:

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?

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.

···

Nathan McCorkle wrote:

    On Sunday, September 7, 2014 3:36:04 PM UTC-7, Paul Thompson

wrote:

        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.

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

The OP said between applications, not Python objects.

···

On Monday, September 8, 2014 10:26:22 AM UTC-7, Tim Roberts wrote:

Nathan McCorkle wrote:

    On Sunday, September 7, 2014 3:36:04 PM UTC-7, Paul Thompson > > wrote:
        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.

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 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
···

On 07/09/14 23:36, Paul Thompson wrote:

    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+unsubscribe@googlegroups.com.

  For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

PubSub

Where did he say that?

···

On Mon, Sep 8, 2014 at 10:50 AM, Nathan McCorkle <nmz787@gmail.com> wrote:

The OP said between applications, not Python objects.

        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
···
    On Sunday, September 7, 2014 3:36:04 PM UTC-7, Paul Thompson > > > wrote:

The title of the post ‘Passing information from one function to another in a large application’

···

On Monday, September 8, 2014 10:54:37 AM UTC-7, Marc wrote:

On Mon, Sep 8, 2014 at 10:50 AM, Nathan McCorkle nmz...@gmail.com wrote:

The OP said between applications, not Python objects.

Where did he say that?

Hi,

···

On Sunday, September 7, 2014 5:36:04 PM UTC-5, Paul Thompson wrote:

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: python - Why are global variables evil? - Stack Overflow

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

Oh, I guess I mis-read between-applications

···

On Monday, September 8, 2014 11:08:03 AM UTC-7, Nathan McCorkle wrote:

On Monday, September 8, 2014 10:54:37 AM UTC-7, Marc wrote:

On Mon, Sep 8, 2014 at 10:50 AM, Nathan McCorkle nmz...@gmail.com wrote:

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’