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