Python Forum
Function Attribute / Global Variable Confusion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function Attribute / Global Variable Confusion
#1
Function attributes and global variables seem quite similar:

def hi():

    hi.bye = 50     # function attribute

hi()

def testing():

    global ok       # global variable
    ok = 50

testing()

print(hi.bye + ok)  # output -> 100 


Can someone explain what the main difference is between the two?

Also PyCharm gives me two warnings in the editor (no errors in output area):

"Global variable 'ok' is undefined at the module level"

"Cannot find reference 'bye' in 'function'"
Reply
#2
This is not possible, because the function object is first evaluated and then the function object which sits somewhere in memory is assigned to the name hi. Inside the function you try to set an attribute on an reference which does not exist.
def hi():
    hi.bye = 50
You can do instead:
def hi():
    pass
hi.bye = 50
print(hi.bye)
In this case the function is created and assigned to the name hi.
Afterwards the integer object with the value 50 is assigned to the function, which refers to the name hi. The dynamic behavior comes from the dict. Look into hi.__dict__ before assignment and after.


The use of global is often a marker for broken code.
Try to avoid it.

# ok does not exist

def testing():
    global ok
    ok = 50

# ok still does not exist.
# now calling the function

testing()

# ok exists now and has the value 50
Using this code style is like guessing. At some point in your program, you lose the overview when which name is assigned.

You should use functions as they made for.

def foo():
    return 42

def bar():
    return 1337

result = foo() + bar()
print(result)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable for the value element in the index function?? Learner1 8 548 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 516 Nov-23-2023, 02:53 PM
Last Post: rob101
  It's saying my global variable is a local variable Radical 5 1,098 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Printing the variable from defined function jws 7 1,166 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 893 Aug-07-2023, 05:58 PM
Last Post: Karp
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,215 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Retrieve variable from function labgoggles 2 1,000 Jul-01-2022, 07:23 PM
Last Post: labgoggles
  AttributeError: 'function' object has no attribute 'metadata 3lnyn0 5 4,522 Mar-28-2022, 04:42 PM
Last Post: Larz60+
  Scope of variable confusion Mark17 10 2,740 Feb-24-2022, 06:03 PM
Last Post: deanhystad
  Cant transfer a variable onto another function KEIKAS 5 1,836 Feb-09-2022, 10:17 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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