__name__ and __qualname__ missing in object
Question:
I have a deeply nested object and recently I found out that people are performing __qualname__
to get the path of nested object but mine seem to be missing this two magic function. I am currently using Python 3.6.
object class
class node:
def __init__(self, dict1):
self.__dict__.update(dict1)
Function to convert dictionary to nested objects:
def dict2obj(dict1):
return json.loads(json.dumps(dict1), object_hook=node)
load json to dictionary and convert it:
abc = dict2obj(json.load(open("example.json")))
The whole thing convert JSON file to nested objects, so I can navigate through the object like abc.foo.bar
and I would like to perform abc.foo.bar.__name__
which return 'bar'
and abc.foo.bar.__qualname__
which return 'abc.foo.bar'
but both of this magic function seems to be missing.
Answers:
There can never be such a function in python. When you define a class or function, __name__
is set to the name of the function:
class A:
pass
A.__name__
# prints "A"
The class
keyword sets this variable, it is not a function and won’t be set later if you assign A to another variable:
class A:
pass
B = A
B.__name__
# Still prints "A", since only the class keyword sets name, not reassignment
Qualname
A module object also has a __name__
set by import, that is why you see if __name__ == "__main__"
for executable python scripts to make sure they are not executed if imported.
If you have a file like this:
a.py
print __name__
b.py
import a
This will print a
.
If you define a class in a.py
, the __qualname__
will be the global module level variable __name__
concatenated with the classes own __name__
a.py
class A:
pass
print(A.__qualname__)
b.py
import a
This will print a.A
Why it is impossible for such a variable to exist
Any object can have multiple references. Which name to use? Assume a function get_full_path
existed that could do what you wanted.
def k(m):
print(get_full_path(m.four[2])) # no such function exists
a = db.users["bob"].name_field
b.all_names = a.names
k(b.all_names[5])
What should this print? m.four[2]
or db.users["bob"].name_field.names.four[2]
or b.all_names.four[2]
? They are all equally valid and there is no difference between them.
I have a deeply nested object and recently I found out that people are performing __qualname__
to get the path of nested object but mine seem to be missing this two magic function. I am currently using Python 3.6.
object class
class node:
def __init__(self, dict1):
self.__dict__.update(dict1)
Function to convert dictionary to nested objects:
def dict2obj(dict1):
return json.loads(json.dumps(dict1), object_hook=node)
load json to dictionary and convert it:
abc = dict2obj(json.load(open("example.json")))
The whole thing convert JSON file to nested objects, so I can navigate through the object like abc.foo.bar
and I would like to perform abc.foo.bar.__name__
which return 'bar'
and abc.foo.bar.__qualname__
which return 'abc.foo.bar'
but both of this magic function seems to be missing.
There can never be such a function in python. When you define a class or function, __name__
is set to the name of the function:
class A:
pass
A.__name__
# prints "A"
The class
keyword sets this variable, it is not a function and won’t be set later if you assign A to another variable:
class A:
pass
B = A
B.__name__
# Still prints "A", since only the class keyword sets name, not reassignment
Qualname
A module object also has a __name__
set by import, that is why you see if __name__ == "__main__"
for executable python scripts to make sure they are not executed if imported.
If you have a file like this:
a.py
print __name__
b.py
import a
This will print a
.
If you define a class in a.py
, the __qualname__
will be the global module level variable __name__
concatenated with the classes own __name__
a.py
class A:
pass
print(A.__qualname__)
b.py
import a
This will print a.A
Why it is impossible for such a variable to exist
Any object can have multiple references. Which name to use? Assume a function get_full_path
existed that could do what you wanted.
def k(m):
print(get_full_path(m.four[2])) # no such function exists
a = db.users["bob"].name_field
b.all_names = a.names
k(b.all_names[5])
What should this print? m.four[2]
or db.users["bob"].name_field.names.four[2]
or b.all_names.four[2]
? They are all equally valid and there is no difference between them.