Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable Scope for Scripts
#4
(Dec-01-2017, 02:59 PM)QueenSvetlana Wrote: The way I've declared file_name and files_to_backup are not in a global way, correct?
That's not correct the are in Global namespace,
so the are global variables in that code,
but they are fine as the are the last step and can not interfere with other stuff.

To take a look,i do this so only new stuff in Global namespace is shown so it don't look so messy.
>>> from pprint import pprint

>>> def read_data_from_file(file_name):
    '''In Local namespace'''
    foo = '/bar'
    bar = 100
    return file_name + foo

# We are in Global namespace so these are global variables
>>> file_name = "some/path/to/file"
>>> files_to_backup = read_data_from_file(file_name)

>>> pprint({key: val for key, val in globals().items() if '__' not in key})
{'file_name': 'some/path/to/file',
 'files_to_backup': 'some/path/to/file/bar',
 'pprint': <function pprint at 0x03454CD8>,
 'read_data_from_file': <function read_data_from_file at 0x03454C90>}
See that foo and bar is not seen in Global namespace,have to call function read_data_from_file.
QueenSvetlana Wrote:The problem is these two variables might be used else where in my script, keeping them hidden inside a function will make it more difficult to use. You could return a dictionary of items, but it seems unnecessarily complicated. 
It's not so complicated you make the call  when needed or function can also bye passed to other function.
If all this get to complicated,then it can be time to think of using Classes to structure code.
QueenSvetlana Wrote:The only way to hide a variable is inside a function.
Both function and classes(own namespace),keep variables local so they don't pollute Global namespace.
Reply


Messages In This Thread
Variable Scope for Scripts - by QueenSvetlana - Dec-01-2017, 03:34 AM
RE: Variable Scope for Scripts - by snippsat - Dec-01-2017, 02:40 PM
RE: Variable Scope for Scripts - by QueenSvetlana - Dec-01-2017, 02:59 PM
RE: Variable Scope for Scripts - by snippsat - Dec-01-2017, 06:01 PM
RE: Variable Scope for Scripts - by QueenSvetlana - Dec-01-2017, 07:40 PM
RE: Variable Scope for Scripts - by RickyWilson - Dec-01-2017, 06:26 PM
RE: Variable Scope for Scripts - by snippsat - Dec-01-2017, 08:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,955 Nov-07-2023, 09:49 AM
Last Post: buran
  Library scope mike_zah 2 884 Feb-23-2023, 12:20 AM
Last Post: mike_zah
  Scope of variable confusion Mark17 10 2,947 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Variable scope issue melvin13 2 1,610 Nov-29-2021, 08:26 PM
Last Post: melvin13
  Variable scope - "global x" didn't work... ptrivino 5 3,117 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Python Closures and Scope muzikman 2 1,881 Dec-14-2020, 11:21 PM
Last Post: muzikman
  Block of code, scope of variables and surprising exception arbiel 8 3,500 Apr-06-2020, 07:57 PM
Last Post: arbiel
  Help with Global/Coerced Variable (Understanding Scope) Rev2k 6 3,594 Jan-09-2020, 03:43 AM
Last Post: Rev2k
  Solving a scope issue profconn1 4 2,672 Nov-01-2019, 07:46 PM
Last Post: profconn1
  Namespace and scope difference Uchikago 9 4,696 Jul-03-2019, 03:36 PM
Last Post: Uchikago

Forum Jump:

User Panel Messages

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