Why does an imported function “as” another name keep its original __name__?
up vote
13
down vote
favorite
Here:
from os.path import exists as foo
print foo.__name__
we get: 'exists'
.
Why not 'foo'
? Which attribute would give 'foo'
?
python function module python-import
add a comment |
up vote
13
down vote
favorite
Here:
from os.path import exists as foo
print foo.__name__
we get: 'exists'
.
Why not 'foo'
? Which attribute would give 'foo'
?
python function module python-import
3
from X import Y as Z
is equivalent tofrom X import Y; Z=Y; del Y
. The only difference is that it does not hide something calledY
that already existed before the import and if it existed is not deleted (Y="Hi"; from X import Y as Z
thenY == "Hi"
).
– Giacomo Alzetta
Nov 13 at 14:48
add a comment |
up vote
13
down vote
favorite
up vote
13
down vote
favorite
Here:
from os.path import exists as foo
print foo.__name__
we get: 'exists'
.
Why not 'foo'
? Which attribute would give 'foo'
?
python function module python-import
Here:
from os.path import exists as foo
print foo.__name__
we get: 'exists'
.
Why not 'foo'
? Which attribute would give 'foo'
?
python function module python-import
python function module python-import
asked Nov 13 at 11:56
Basj
5,24528100211
5,24528100211
3
from X import Y as Z
is equivalent tofrom X import Y; Z=Y; del Y
. The only difference is that it does not hide something calledY
that already existed before the import and if it existed is not deleted (Y="Hi"; from X import Y as Z
thenY == "Hi"
).
– Giacomo Alzetta
Nov 13 at 14:48
add a comment |
3
from X import Y as Z
is equivalent tofrom X import Y; Z=Y; del Y
. The only difference is that it does not hide something calledY
that already existed before the import and if it existed is not deleted (Y="Hi"; from X import Y as Z
thenY == "Hi"
).
– Giacomo Alzetta
Nov 13 at 14:48
3
3
from X import Y as Z
is equivalent to from X import Y; Z=Y; del Y
. The only difference is that it does not hide something called Y
that already existed before the import and if it existed is not deleted (Y="Hi"; from X import Y as Z
then Y == "Hi"
).– Giacomo Alzetta
Nov 13 at 14:48
from X import Y as Z
is equivalent to from X import Y; Z=Y; del Y
. The only difference is that it does not hide something called Y
that already existed before the import and if it existed is not deleted (Y="Hi"; from X import Y as Z
then Y == "Hi"
).– Giacomo Alzetta
Nov 13 at 14:48
add a comment |
3 Answers
3
active
oldest
votes
up vote
32
down vote
accepted
You can view import foo as bar
as just an assignment. You would not expect a function to change its name when you assign another name to it.
>>> def foo(): pass
>>>
>>> foo.__name__
>>> 'foo'
>>> bar = foo
>>> bar.__name__
>>> 'foo'
Thanks. What attribute of the variable
bar
would return the string'bar'
then?
There is no such attribute. Names (bar
) refer to values (the function object) unidirectionally.
The __name__
attribute of a function is set as the name the function was defined with using thedef ...
syntax. That's why you don't get a meaningful __name__
attribute if you define an anonymous function and assign the name foo
after it has been created.
>>> foo = lambda: None
>>> foo.__name__
>>> '<lambda>'
@Basj there is no such attribute. Names (bar
) refer to values (the function) unidirectionally.
– timgeb
Nov 13 at 11:59
3
@Basj It is not strange at all. Python names are just references to memory locations.bar = 'a string'
, then'a string'
has no clue that it happens to be referenced bybar
, so'a string'.__name__
can never be'bar'
– DeepSpace
Nov 13 at 12:03
@DeepSpace Ok. I thought that there would be a duplicate "object"bar
that would just make the link tofoo
, and that would have its proper__name__
.
– Basj
Nov 13 at 12:05
@Basj Note that what you suggest would imply that: 1) the function object has to somehow be modified each time its reference is assigned or unassigned to a new variable name 2) if you assign multiple variable names to the same function reference you would have either make copies of the function or somehow keep track of all the variable names
– jdehesa
Nov 13 at 12:05
1
Another way to see this is that the same function could be referred to by multiple variables. Clearly, it can only have one name. The name is a property of the function, not of the variable.
– usr
Nov 13 at 19:27
|
show 2 more comments
up vote
14
down vote
Importing an object just binds a new variable, and all that adding as newname
does is let you pick an alternative name to use for the variable in the current namespace.
The __name__
attribute on an object says nothing about the name it is currently bound to, you can have any number of variables as well as containers such as lists or dictionaries pointing to the same object, after all:
def foo(): pass
bar = foo
spam = foo
list_of_functions = [foo]
dictionary_of_functions = {'monty': foo, 'python': foo}
The above created 4 additional references to the function object; you can't have foo.__name__
reflect all of those, and the references in list_of_functions
and dictionary_of_functions
do not (directly) have names.
Since import foo
, import bar as foo
, from module import foo
and from module import bar as foo
all just set the name foo
in the current module, they are treated the exact same way as other assignments. You could import the function more than once, under different names, too.
Instead, the __name__
value of a function is set to name it was defined with in the def <name>(...):
statement. It is a debugging aid, at most. It is used in tracebacks, for example, to make it easier to identify lines of code shown in the traceback. You'd only set the __name__
to something else if that would help identify the location better. (Note: in Python 3, there is also the __qualname_
attribute, which is used instead of __name__
as it includes more information on where the function is defined when nested or defined on a class).
add a comment |
up vote
0
down vote
The as
is syntactical sugar in the file/session of the import, while the __name__
attribute is part of the function object.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
32
down vote
accepted
You can view import foo as bar
as just an assignment. You would not expect a function to change its name when you assign another name to it.
>>> def foo(): pass
>>>
>>> foo.__name__
>>> 'foo'
>>> bar = foo
>>> bar.__name__
>>> 'foo'
Thanks. What attribute of the variable
bar
would return the string'bar'
then?
There is no such attribute. Names (bar
) refer to values (the function object) unidirectionally.
The __name__
attribute of a function is set as the name the function was defined with using thedef ...
syntax. That's why you don't get a meaningful __name__
attribute if you define an anonymous function and assign the name foo
after it has been created.
>>> foo = lambda: None
>>> foo.__name__
>>> '<lambda>'
@Basj there is no such attribute. Names (bar
) refer to values (the function) unidirectionally.
– timgeb
Nov 13 at 11:59
3
@Basj It is not strange at all. Python names are just references to memory locations.bar = 'a string'
, then'a string'
has no clue that it happens to be referenced bybar
, so'a string'.__name__
can never be'bar'
– DeepSpace
Nov 13 at 12:03
@DeepSpace Ok. I thought that there would be a duplicate "object"bar
that would just make the link tofoo
, and that would have its proper__name__
.
– Basj
Nov 13 at 12:05
@Basj Note that what you suggest would imply that: 1) the function object has to somehow be modified each time its reference is assigned or unassigned to a new variable name 2) if you assign multiple variable names to the same function reference you would have either make copies of the function or somehow keep track of all the variable names
– jdehesa
Nov 13 at 12:05
1
Another way to see this is that the same function could be referred to by multiple variables. Clearly, it can only have one name. The name is a property of the function, not of the variable.
– usr
Nov 13 at 19:27
|
show 2 more comments
up vote
32
down vote
accepted
You can view import foo as bar
as just an assignment. You would not expect a function to change its name when you assign another name to it.
>>> def foo(): pass
>>>
>>> foo.__name__
>>> 'foo'
>>> bar = foo
>>> bar.__name__
>>> 'foo'
Thanks. What attribute of the variable
bar
would return the string'bar'
then?
There is no such attribute. Names (bar
) refer to values (the function object) unidirectionally.
The __name__
attribute of a function is set as the name the function was defined with using thedef ...
syntax. That's why you don't get a meaningful __name__
attribute if you define an anonymous function and assign the name foo
after it has been created.
>>> foo = lambda: None
>>> foo.__name__
>>> '<lambda>'
@Basj there is no such attribute. Names (bar
) refer to values (the function) unidirectionally.
– timgeb
Nov 13 at 11:59
3
@Basj It is not strange at all. Python names are just references to memory locations.bar = 'a string'
, then'a string'
has no clue that it happens to be referenced bybar
, so'a string'.__name__
can never be'bar'
– DeepSpace
Nov 13 at 12:03
@DeepSpace Ok. I thought that there would be a duplicate "object"bar
that would just make the link tofoo
, and that would have its proper__name__
.
– Basj
Nov 13 at 12:05
@Basj Note that what you suggest would imply that: 1) the function object has to somehow be modified each time its reference is assigned or unassigned to a new variable name 2) if you assign multiple variable names to the same function reference you would have either make copies of the function or somehow keep track of all the variable names
– jdehesa
Nov 13 at 12:05
1
Another way to see this is that the same function could be referred to by multiple variables. Clearly, it can only have one name. The name is a property of the function, not of the variable.
– usr
Nov 13 at 19:27
|
show 2 more comments
up vote
32
down vote
accepted
up vote
32
down vote
accepted
You can view import foo as bar
as just an assignment. You would not expect a function to change its name when you assign another name to it.
>>> def foo(): pass
>>>
>>> foo.__name__
>>> 'foo'
>>> bar = foo
>>> bar.__name__
>>> 'foo'
Thanks. What attribute of the variable
bar
would return the string'bar'
then?
There is no such attribute. Names (bar
) refer to values (the function object) unidirectionally.
The __name__
attribute of a function is set as the name the function was defined with using thedef ...
syntax. That's why you don't get a meaningful __name__
attribute if you define an anonymous function and assign the name foo
after it has been created.
>>> foo = lambda: None
>>> foo.__name__
>>> '<lambda>'
You can view import foo as bar
as just an assignment. You would not expect a function to change its name when you assign another name to it.
>>> def foo(): pass
>>>
>>> foo.__name__
>>> 'foo'
>>> bar = foo
>>> bar.__name__
>>> 'foo'
Thanks. What attribute of the variable
bar
would return the string'bar'
then?
There is no such attribute. Names (bar
) refer to values (the function object) unidirectionally.
The __name__
attribute of a function is set as the name the function was defined with using thedef ...
syntax. That's why you don't get a meaningful __name__
attribute if you define an anonymous function and assign the name foo
after it has been created.
>>> foo = lambda: None
>>> foo.__name__
>>> '<lambda>'
edited Nov 13 at 12:06
answered Nov 13 at 11:57
timgeb
43.8k106085
43.8k106085
@Basj there is no such attribute. Names (bar
) refer to values (the function) unidirectionally.
– timgeb
Nov 13 at 11:59
3
@Basj It is not strange at all. Python names are just references to memory locations.bar = 'a string'
, then'a string'
has no clue that it happens to be referenced bybar
, so'a string'.__name__
can never be'bar'
– DeepSpace
Nov 13 at 12:03
@DeepSpace Ok. I thought that there would be a duplicate "object"bar
that would just make the link tofoo
, and that would have its proper__name__
.
– Basj
Nov 13 at 12:05
@Basj Note that what you suggest would imply that: 1) the function object has to somehow be modified each time its reference is assigned or unassigned to a new variable name 2) if you assign multiple variable names to the same function reference you would have either make copies of the function or somehow keep track of all the variable names
– jdehesa
Nov 13 at 12:05
1
Another way to see this is that the same function could be referred to by multiple variables. Clearly, it can only have one name. The name is a property of the function, not of the variable.
– usr
Nov 13 at 19:27
|
show 2 more comments
@Basj there is no such attribute. Names (bar
) refer to values (the function) unidirectionally.
– timgeb
Nov 13 at 11:59
3
@Basj It is not strange at all. Python names are just references to memory locations.bar = 'a string'
, then'a string'
has no clue that it happens to be referenced bybar
, so'a string'.__name__
can never be'bar'
– DeepSpace
Nov 13 at 12:03
@DeepSpace Ok. I thought that there would be a duplicate "object"bar
that would just make the link tofoo
, and that would have its proper__name__
.
– Basj
Nov 13 at 12:05
@Basj Note that what you suggest would imply that: 1) the function object has to somehow be modified each time its reference is assigned or unassigned to a new variable name 2) if you assign multiple variable names to the same function reference you would have either make copies of the function or somehow keep track of all the variable names
– jdehesa
Nov 13 at 12:05
1
Another way to see this is that the same function could be referred to by multiple variables. Clearly, it can only have one name. The name is a property of the function, not of the variable.
– usr
Nov 13 at 19:27
@Basj there is no such attribute. Names (
bar
) refer to values (the function) unidirectionally.– timgeb
Nov 13 at 11:59
@Basj there is no such attribute. Names (
bar
) refer to values (the function) unidirectionally.– timgeb
Nov 13 at 11:59
3
3
@Basj It is not strange at all. Python names are just references to memory locations.
bar = 'a string'
, then 'a string'
has no clue that it happens to be referenced by bar
, so 'a string'.__name__
can never be 'bar'
– DeepSpace
Nov 13 at 12:03
@Basj It is not strange at all. Python names are just references to memory locations.
bar = 'a string'
, then 'a string'
has no clue that it happens to be referenced by bar
, so 'a string'.__name__
can never be 'bar'
– DeepSpace
Nov 13 at 12:03
@DeepSpace Ok. I thought that there would be a duplicate "object"
bar
that would just make the link to foo
, and that would have its proper __name__
.– Basj
Nov 13 at 12:05
@DeepSpace Ok. I thought that there would be a duplicate "object"
bar
that would just make the link to foo
, and that would have its proper __name__
.– Basj
Nov 13 at 12:05
@Basj Note that what you suggest would imply that: 1) the function object has to somehow be modified each time its reference is assigned or unassigned to a new variable name 2) if you assign multiple variable names to the same function reference you would have either make copies of the function or somehow keep track of all the variable names
– jdehesa
Nov 13 at 12:05
@Basj Note that what you suggest would imply that: 1) the function object has to somehow be modified each time its reference is assigned or unassigned to a new variable name 2) if you assign multiple variable names to the same function reference you would have either make copies of the function or somehow keep track of all the variable names
– jdehesa
Nov 13 at 12:05
1
1
Another way to see this is that the same function could be referred to by multiple variables. Clearly, it can only have one name. The name is a property of the function, not of the variable.
– usr
Nov 13 at 19:27
Another way to see this is that the same function could be referred to by multiple variables. Clearly, it can only have one name. The name is a property of the function, not of the variable.
– usr
Nov 13 at 19:27
|
show 2 more comments
up vote
14
down vote
Importing an object just binds a new variable, and all that adding as newname
does is let you pick an alternative name to use for the variable in the current namespace.
The __name__
attribute on an object says nothing about the name it is currently bound to, you can have any number of variables as well as containers such as lists or dictionaries pointing to the same object, after all:
def foo(): pass
bar = foo
spam = foo
list_of_functions = [foo]
dictionary_of_functions = {'monty': foo, 'python': foo}
The above created 4 additional references to the function object; you can't have foo.__name__
reflect all of those, and the references in list_of_functions
and dictionary_of_functions
do not (directly) have names.
Since import foo
, import bar as foo
, from module import foo
and from module import bar as foo
all just set the name foo
in the current module, they are treated the exact same way as other assignments. You could import the function more than once, under different names, too.
Instead, the __name__
value of a function is set to name it was defined with in the def <name>(...):
statement. It is a debugging aid, at most. It is used in tracebacks, for example, to make it easier to identify lines of code shown in the traceback. You'd only set the __name__
to something else if that would help identify the location better. (Note: in Python 3, there is also the __qualname_
attribute, which is used instead of __name__
as it includes more information on where the function is defined when nested or defined on a class).
add a comment |
up vote
14
down vote
Importing an object just binds a new variable, and all that adding as newname
does is let you pick an alternative name to use for the variable in the current namespace.
The __name__
attribute on an object says nothing about the name it is currently bound to, you can have any number of variables as well as containers such as lists or dictionaries pointing to the same object, after all:
def foo(): pass
bar = foo
spam = foo
list_of_functions = [foo]
dictionary_of_functions = {'monty': foo, 'python': foo}
The above created 4 additional references to the function object; you can't have foo.__name__
reflect all of those, and the references in list_of_functions
and dictionary_of_functions
do not (directly) have names.
Since import foo
, import bar as foo
, from module import foo
and from module import bar as foo
all just set the name foo
in the current module, they are treated the exact same way as other assignments. You could import the function more than once, under different names, too.
Instead, the __name__
value of a function is set to name it was defined with in the def <name>(...):
statement. It is a debugging aid, at most. It is used in tracebacks, for example, to make it easier to identify lines of code shown in the traceback. You'd only set the __name__
to something else if that would help identify the location better. (Note: in Python 3, there is also the __qualname_
attribute, which is used instead of __name__
as it includes more information on where the function is defined when nested or defined on a class).
add a comment |
up vote
14
down vote
up vote
14
down vote
Importing an object just binds a new variable, and all that adding as newname
does is let you pick an alternative name to use for the variable in the current namespace.
The __name__
attribute on an object says nothing about the name it is currently bound to, you can have any number of variables as well as containers such as lists or dictionaries pointing to the same object, after all:
def foo(): pass
bar = foo
spam = foo
list_of_functions = [foo]
dictionary_of_functions = {'monty': foo, 'python': foo}
The above created 4 additional references to the function object; you can't have foo.__name__
reflect all of those, and the references in list_of_functions
and dictionary_of_functions
do not (directly) have names.
Since import foo
, import bar as foo
, from module import foo
and from module import bar as foo
all just set the name foo
in the current module, they are treated the exact same way as other assignments. You could import the function more than once, under different names, too.
Instead, the __name__
value of a function is set to name it was defined with in the def <name>(...):
statement. It is a debugging aid, at most. It is used in tracebacks, for example, to make it easier to identify lines of code shown in the traceback. You'd only set the __name__
to something else if that would help identify the location better. (Note: in Python 3, there is also the __qualname_
attribute, which is used instead of __name__
as it includes more information on where the function is defined when nested or defined on a class).
Importing an object just binds a new variable, and all that adding as newname
does is let you pick an alternative name to use for the variable in the current namespace.
The __name__
attribute on an object says nothing about the name it is currently bound to, you can have any number of variables as well as containers such as lists or dictionaries pointing to the same object, after all:
def foo(): pass
bar = foo
spam = foo
list_of_functions = [foo]
dictionary_of_functions = {'monty': foo, 'python': foo}
The above created 4 additional references to the function object; you can't have foo.__name__
reflect all of those, and the references in list_of_functions
and dictionary_of_functions
do not (directly) have names.
Since import foo
, import bar as foo
, from module import foo
and from module import bar as foo
all just set the name foo
in the current module, they are treated the exact same way as other assignments. You could import the function more than once, under different names, too.
Instead, the __name__
value of a function is set to name it was defined with in the def <name>(...):
statement. It is a debugging aid, at most. It is used in tracebacks, for example, to make it easier to identify lines of code shown in the traceback. You'd only set the __name__
to something else if that would help identify the location better. (Note: in Python 3, there is also the __qualname_
attribute, which is used instead of __name__
as it includes more information on where the function is defined when nested or defined on a class).
edited Nov 13 at 12:15
answered Nov 13 at 12:03
Martijn Pieters♦
689k12723782226
689k12723782226
add a comment |
add a comment |
up vote
0
down vote
The as
is syntactical sugar in the file/session of the import, while the __name__
attribute is part of the function object.
add a comment |
up vote
0
down vote
The as
is syntactical sugar in the file/session of the import, while the __name__
attribute is part of the function object.
add a comment |
up vote
0
down vote
up vote
0
down vote
The as
is syntactical sugar in the file/session of the import, while the __name__
attribute is part of the function object.
The as
is syntactical sugar in the file/session of the import, while the __name__
attribute is part of the function object.
answered Nov 13 at 12:01
halloleo
2,30942257
2,30942257
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280530%2fwhy-does-an-imported-function-as-another-name-keep-its-original-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
from X import Y as Z
is equivalent tofrom X import Y; Z=Y; del Y
. The only difference is that it does not hide something calledY
that already existed before the import and if it existed is not deleted (Y="Hi"; from X import Y as Z
thenY == "Hi"
).– Giacomo Alzetta
Nov 13 at 14:48