Python Forum
Global Variables - Some Points Needing Clarification.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global Variables - Some Points Needing Clarification.
#1
Note: This is in context of a self contained module, having some functions which are not meant to be called by any other module.

As per recommended practice, use of module level variables in an assignment statement within a function, needing prior declaration as global. is to be avoided.

Could it be presumed that there would be no objection to use of other such variables within the function when manipulation of their values is not involved and global declaration is not needed?

In case of object variables serving as pointers to lists, dictionaries etc, the base object does get affected, even if the reference is passed to the function under a different name. As a result, the effect is global, even without any global declaration.

Under the circumstances, your views are requested whether there could be any grounds against:
(a) Passing object variable reference as function argument, without changing the name of such variables.
OR
(b) Using the module level object variable name without explicitly passing it to the function as an argument.
A.D.Tejpal
Reply
#2
I think strategy (b) is perfectly acceptable. There are examples in the standard library. For example with the logging module, you can do
>>> import logging                                                                           
>>> logging.basicConfig(level=logging.DEBUG)                                                 
>>> logging.info('hello world')                                                              
INFO:root:hello world
The function logging.info() uses a global object, the root logger corresponding to the variable logging.root. As you can see, the object is not passed as argument.

In this case, the user can define other loggers and use their info() method, which does pass the logger argument as the self argument, for example
>>> spam = logging.getLogger('Spam')
>>> spam.info('Hi there')
INFO:Spam:Hi there
Internally, the function logging.info() only calls the method on the global root logger, root.info().

This example also demonstrates that instead of passing explicitly the global object in the functions' signature, the solution is often to make the functions methods of a class and to make the global object a member of the class, or an instance, so that the pointer to this object is passed implicitly through the self argument.
Reply
#3
(Nov-30-2019, 07:58 AM)Gribouillis Wrote: I think strategy (b) is perfectly acceptable.
Thanks for your kind confirmation.

Regarding the suggestion:
Quote:instead of passing explicitly the global object in the functions' signature, the solution is often to make the functions methods of a class and to make the global object a member of the class, or an instance, so that the pointer to this object is passed implicitly through the self argument.

Would setting up a class be the preferred option even if it were to be called just for a single instance?
A.D.Tejpal
Reply
#4
adt Wrote:Would setting up a class be the preferred option even if it were to be called just for a single instance?
Some global objects don't need a special class. For example tokenize.tok_name is an ordinary dictionary. There are many examples where the python FDTs are sufficient.

I can only speak for myself, but I think a class is an elementary component and it can be easily created even for a single instance. A class allows several methods to share access the instance's members. Furthermore it allows to hide several global variables as members of the instance or the class.
Reply
#5
(Nov-30-2019, 12:31 PM)Gribouillis Wrote: a class is an elementary component and it can be easily created even for a single instance

My sincere thanks for your kind advice. I would try to adapt the current project accordingly, using a wrapper class.
A.D.Tejpal
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  needing some help to write some code for a game calculator rymdaksel 1 357 Jan-02-2024, 09:56 AM
Last Post: deanhystad
  Trying to understand global variables 357mag 5 1,065 May-12-2023, 04:16 PM
Last Post: deanhystad
  Can I get some clarification on importing functions from external files. wh33t 3 859 Feb-25-2023, 08:07 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 985 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,102 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 905 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  Looking for clarification related to performance Pymon 5 1,996 Apr-04-2022, 04:47 PM
Last Post: deanhystad
  Needing to Check Every Quarter Hour bill_z 10 2,898 Feb-09-2022, 07:23 PM
Last Post: menator01
  Read Tensorflow Documentation - Clarification IoannisDem 0 1,156 Aug-20-2021, 10:36 AM
Last Post: IoannisDem
  *args implementation and clarification about tuple status amjass12 10 3,930 Jul-07-2021, 10:29 AM
Last Post: amjass12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020