Python Forum
Where to put the global keyword when assigning variables outside a function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where to put the global keyword when assigning variables outside a function?
#6
(Feb-08-2020, 01:23 PM)metulburr Wrote: i have been using python now for about 10 years. 10 years ago i learned about globals, but i seriously forgot most of it because i never use them. C/C++ is not Python, it is not good practice in Python to use globals. That said, if your just trying to understand the concept, thats fine. I just figured i would reverberate the fact globals can be coded a different way.

Thanks. Yes, I am just trying to understand the concept.

(Feb-08-2020, 02:54 AM)Larz60+ Wrote: globals are almost never necessary.
If you feel you need them, rethink your coding
def bind_a_variable():
    alist = []
    return alist

a = bind_a_variable()
of course in reality, you would code this simply as:
a = []

By "of course in reality, you would code this simply as:
a = []
[/quote]"

Do you mean either of these two blocks of code will work?

In C, we need to define the function first at the beginning of the program before calling it (e.g. in main).
Alternatively, if the programmer define the function at the end of the program (after the function is called by main for example), the programmer needs to write down the prototype of the function at the beginning of the program. How about python?
Reply


Messages In This Thread
RE: Where to put the global keyword when assigning variables outside a function? - by new_to_python - Feb-08-2020, 02:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't list require global keyword? johnywhy 9 798 Jan-15-2024, 11:47 PM
Last Post: sgrey
  Find a specific keyword after another keyword and change the output sgtmcc 5 808 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  Trying to understand global variables 357mag 5 1,116 May-12-2023, 04:16 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 1,021 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,143 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  How to print variables in function? samuelbachorik 3 902 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  Clarity on global variables JonWayn 2 945 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  i want to use type= as a function/method keyword argument Skaperen 9 1,838 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  User-defined function to reset variables? Mark17 3 1,644 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Function global not readable by 'main' fmr300 1 1,337 Jan-16-2022, 01:18 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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