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?
#1
Hello, I came across the following code:

a = None

def bind_a_variable():
  global a
  a = []

bind_a_variable()
It is stated that assigning variables outside of the function's scope is possible but those variables must be declared as global via the global keyboard. I am a C programmer. In C, we have to declare a variable first before using it. I would declare global a before
a = None
. However, in the example, a is declared inside a function and after the
a = None 
assignment. Could anybody please clarify?
Reply
#2
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 = []
Reply
#3
(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 = []

Thanks. In regard to globals are almost never necessary, is it the same reason as other languages such as C and C++?

Anyway, I am trying to understand some examples from teaching materials.
Reply
#4
Quote:In regard to globals are almost never necessary, is it the same reason as other languages such as C and C++?
I was a 'C' (and sometimes C++) programmer for 30 years.
I don't think I ever used a Global during that time.

If you have teaching material and their showing this (globals) as valid code, find some other material!
If there just showing how it's done, and recommending against it, that's OK.
Reply
#5
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.
Reply
#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
#7
a = []
is the proper way!

otherwise it's like you have a company that make teapots, but before you sell them,
you move them to someone else's factory next door, then have them return them to you,
telling you that they are teapots.
Reply
#8
There is no prototyping. However, you do have to define an object before you call it of course
Reply
#9
(Feb-08-2020, 10:59 PM)metulburr Wrote: There is no prototyping. However, you do have to define an object before you call it of course

Thanks. So, function definition(s) and then the main program.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Global variables not working hobbyist 9 960 Jan-16-2021, 03:17 PM
Last Post: jefsummers
  Global vs. Local Variables Davy_Jones_XIV 4 576 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Finding global extrema of oscillating function JoeRogan 0 410 Dec-22-2020, 01:49 AM
Last Post: JoeRogan
  Global - local variables Motorhomer14 11 876 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  How to make global list inside function CHANKC 6 803 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  Do I have to pass 85 variables to function? Milfredo 10 1,194 Sep-26-2020, 10:13 PM
Last Post: Milfredo
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 446 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  print function help percentage and slash (multiple variables) leodavinci1990 3 731 Aug-10-2020, 02:51 AM
Last Post: bowlofred
  Issues with storing variables outside of a function cerulean747 7 1,125 Apr-30-2020, 08:46 AM
Last Post: DeaD_EyE
  Assigning variables Godserena 4 716 Apr-26-2020, 06:59 AM
Last Post: buran

Forum Jump:

User Panel Messages

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