1

In python when I try this:

admin = "Vaibhav"
def print_admin(default):
    # global admin
    if default == "default":
        print(admin)
    else:
        admin = "other"
        print(admin)
print_admin("")
print_admin("default")

It gives me an error:

error:UnboundLocalError: local variable 'admin' referenced before assignment

If i use the global keyword then it only uses global in both cases. I want to be able to use local("other") if "default" is not given in the parameters and global("Vaibhav") if it is.

This works perfectly in javascript but not in python.

Javascript code for doing the same:

let admin_name = "Vaibhav";

function printAdminName(admin_name_default) {
    if (admin_name_default != "default") {
        let admin_name = "Other";
        console.log(admin_name)
    }
    else {
        console.log(admin_name);
    }
}
printAdminName("default");
printAdminName();

output: Vaibhav other

** I also wanted to know why this works in javascript and not python, since both are very similar languages. I wanted to know what concept makes this difference. As i spent a lot of time to logically figure it out.

Va_M
  • 522
  • 2
  • 5
  • 18

2 Answers2

1

Please refer to the answer provided by @jmd_dk

As @KarlKnechtel has brought up in his comments, my answer is a demonstration of limiting the namespace of a variable rather than making it local.


The old answer:

Talking about the scope of a variable:

admin = "Vaibhav"
def print_admin(default):
    if default == "default":
        ## This is global
        global admin
        print(admin)
    else:
        ## This is local to your function
        print_admin.admin = "other"
        print(print_admin.admin)
print_admin("")
print_admin("default")

Though I don't quite understand why you would want to enroll a global variable in this function, but if you have to for some reason, you could always restrain your local variable with a declaration.

Yunkai Xiao
  • 191
  • 12
  • 1
    This is not a local, but instead an **attribute of** the function (which is an object). it effectively works like a global (i.e. it will not handle recursion elegantly), but in a different namespace. ("All the attribute of a specific object" can be thought of as a namespace - and in fact, globals normally work that way: they're attributes of *the module*, which is *also* an object.) – Karl Knechtel Sep 11 '22 at 06:02
  • @KarlKnechtel I like your answer, years later I've realized the difference between them, and indeed as you brought up, one may still access it outside of the function given the right namespace. – Yunkai Xiao Nov 01 '22 at 07:01
1

In Python, you cannot use the same name to refer to both a local and a global variable. Using e.g. global admin forces all occurrences of admin to refer the the global variable, meaning that changing the value of admin will also change the globally defined admin, which appears to be not what you want.

It is perfectly possible (and Pythonic) to read global variables without writing to them (change them), in which case the global keyword is not needed. Consider

admin = "Vaibhav"
def print_admin(default):
    if default == "default":
        # Read in the global admin
        admin_local = admin
    else:
        admin_local = "other"
    print(admin_local)
print_admin("")
print_admin("default")

Here we distinguish between the global and local version of admin by naming them differently. This is a must when you wish to refer to both as different variables.

jmd_dk
  • 12,125
  • 9
  • 63
  • 94
  • Thank you for your ans (this is my first time asking a Q in SO) !! I think this would really solve the problem. By assigning the global to a new local n then using it. And it would also help in avoiding inadvertently changing the global variables value. – Va_M Mar 29 '18 at 07:30
  • @Vaibhav_M No problem. Welcome to SO. – jmd_dk Mar 29 '18 at 08:15