[Tutor] global variables/constants versus volatile variables/constants

ALAN GAULD alan.gauld at btinternet.com
Fri Jun 13 12:13:49 CEST 2014


CCing the tutor list.
Please use ReplyAll when responding to the list.
 
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/

http://www.flickr.com/photos/alangauldphotos



>________________________________
> From: diliup gabadamudalige <diliupg at gmail.com>
>To: Alan Gauld <alan.gauld at btinternet.com> 
>Sent: Friday, 13 June 2014, 9:32
>Subject: Re: [Tutor] global variables/constants versus volatile variables/constants
> 
>
>
>thanks Alan!
>
>in the program that i am writing i used a class called variables and declare all variables i use in the program in the def __init__ section.
>so all variables which otherwise would have to be declared as global so that they can be modified  by other functions are in a class. 
>Is this ok?
>The strings, lists and dictionaries which are not read only are used as locals in functions and classes.
>If there is a string, list or dict. which is used in several functions is it better to use a global or several locals?
>
>
>
>On Fri, Jun 13, 2014 at 1:25 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>On 13/06/14 08:21, diliup gabadamudalige wrote:
>>
>>
>>In my code there are many dictionaries and lists which are used in
>>>various functions. Is it better/pythonic/efficient to have these inside
>>>the function itself or declared at the beginning of the program in which
>>>case they will be global?
>>>
>>
If you are sharing a set of data structures between several functions thats often a sign they should be in a class. The data then becomes the class instance attributes and the functions become the methods.
>>
>>
>>
>>global constants and variable have memory allocated to them but when
>>>declared inside a function are created on the fly, used and discarded.
>>>
>>
That's not usually the main issue when deciding for global/local.
>>Its more about the maintainability of the code and the over
>>use of side effects which makes the code difficult to read.
>>If the data is read only then things are not so bad and globals
>>can be an OK solution.
>>
>>hth
>>-- 
>>Alan G
>>Author of the Learn to Program web site
>>http://www.alan-g.me.uk/
>>http://www.flickr.com/photos/alangauldphotos
>>
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>To unsubscribe or change subscription options:
>>https://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
>-- 
>
>Diliup Gabadamudalige
>
>http://www.diliupg.com
>http://soft.diliupg.com/
>
>**********************************************************************************************
>This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions.
>**********************************************************************************************
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140613/c3ffde11/attachment.html>


More information about the Tutor mailing list