Hi. What do i put in pace of object to be able to print the object names maditsi_car and maditsi_aero
for v in [maditsi_car, maditsi_aero]:
#print(f"{*object*} is a {v.make} {v.model} ")
v.get_make_model()
v.action()
Hi. What do i put in pace of object to be able to print the object names maditsi_car and maditsi_aero
for v in [maditsi_car, maditsi_aero]:
#print(f"{*object*} is a {v.make} {v.model} ")
v.get_make_model()
v.action()
It is not possible to print the name of the variable.
Why do you want to do that?
Is it that maditsi_car is an important property?
If so you should store it as a string in the object so that you can access it and print it out.
In case this is your actual intension âŚ
If you have, for example,
maditsi_car = 111
print(f'{maditsi_car=}')
this would print
maditsi_car=111
The problem is that they arenât âobject namesâ; theyâre variable names in the program. While the program is running, the actual maditsi_car
object doesnât have the text madisti_car
anywhere inside it. Thatâs only a name used in the source code, so that you can explain to Python which object you mean.
An f-string can print a variable name along with the object in that variable, like f'{maditsi_car=}
. But that only works because the information flows the other way around: you tell Python the variable name, which is enough to know both that text itself, and the object. But if you start with the object, you canât get the variable name back.
Inside your loop, v
is a name for one of the objects in the list (a different one each time). But those objects donât know anything about their names - it doesnât know the maditsi_car
or maditsi_aero
name, and it also doesnât know the v
name. Similarly, the list doesnât know how it was created. It has no memory of the names for the variables that were used in the [maditsi_car, maditsi_aero]
code. In the actual Python instructions to make the list, those names arenât there at all. Theyâre stored separately, in debugging information for the function (or module, if this isnât inside a function).
I just want to know if itâs possible. Found out because of the
Hi there! Its definitely possible, however its more complicated than it should be:
def printinLocals():
foo, var = 4, 5
print(locals())
printinLocals()
Output:
{âfooâ: 4, âvarâ: 5}
This might not be what your looking for though, there is such a thing as inspect, and stack.
from inspect import stack
frames = stack()
frame = frames[0]
frameObj = frame[0]
In python27 though Im not sure how tis in python3 but simmilar, and the frame object has a bunch of info on a bunch of stuff including the var names, and I have the hunch that from there you can extract what youre looking for. Also the frame itself has a bunch of stuff and its just a tuple, although I think in python3 its different.
P.D Actually read the full question and its about printing the name of an object, ok, in that case the object should have an instance attribute with the name, and you should define a def repr inside the object definition (class) so you can just do print(obj) and print the name of the object and all you like. Also if I were you and youâre using latest version of python I would use @dataclass decorator for the class which actually defines all this automatically, earlier versions have a lib that does the same but forgot the name