Passing information from one function to another in a large application

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing information from one function to another in a large application

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 email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Passing information from one function to another in a large application

Michael Moriarity

On Sun, Sep 7, 2014 at 6:36 PM, Paul Thompson <[hidden email]> 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 [hidden email].
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Passing information from one function to another in a large application

Rufus V. Smith
In reply to this post by Paul Thompson
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 [hidden email].
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Passing information from one function to another in a large application

Nathan McCorkle
In reply to this post by Paul Thompson


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. 

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Passing information from one function to another in a large application

Tim Roberts
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.
-- 
Tim Roberts, [hidden email]
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Passing information from one function to another in a large application

Nathan McCorkle


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.


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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Passing information from one function to another in a large application

GadgetSteve
In reply to this post by Paul Thompson
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 [hidden email].
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 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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Passing information from one function to another in a large application

Marc Tompkins
In reply to this post by Nathan McCorkle
On Mon, Sep 8, 2014 at 10:50 AM, Nathan McCorkle <[hidden email]> wrote:
> 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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Passing information from one function to another in a large application

Mike Driscoll
In reply to this post by Nathan McCorkle



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.


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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Passing information from one function to another in a large application

Nathan McCorkle
In reply to this post by Marc Tompkins


On Monday, September 8, 2014 10:54:37 AM UTC-7, Marc wrote:
On Mon, Sep 8, 2014 at 10:50 AM, Nathan McCorkle <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="GmNBxd1Yg0wJ" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">nmz...@...> 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' 

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Passing information from one function to another in a large application

Mike Driscoll
In reply to this post by Paul Thompson
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: 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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Passing information from one function to another in a large application

Nathan McCorkle
In reply to this post by Nathan McCorkle


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 <[hidden email]> 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' 

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 [hidden email].
For more options, visit https://groups.google.com/d/optout.