Python Forum

Full Version: How to print variable name?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I want to calculate the lenght of a segment and then to print "The lenght of AB is 27 cm" for example.
In wich the name AB are the names of two variables - point A and point B (Actually A and B are vectors but the point is how to print their names, not how to operate with them)

And to be appliable in function.
Something like:
lenght(A,B)
Could not finish editing the post (there was a time limit)
So I continue:

Something like:
def name(Whatever)#this is the function I'm looking for. Gives me the name of the variable. In this case - Whatever
  ???

def lenght(x,y)
  ...some calculations...
  print('The lenght of', name(x), name(y), 'is', ..the result of the calculations...)
And then to call lenght with A and B:

lenght(A,B)
The result should be:

The lenght of AB is 27 cm
length is len, not lenght
rather than 'something like' make an effort and actually try to get some working code.
There is an almost working code.

def name(obj, callingLocals=locals()):
    for k, v in list(callingLocals.items()):
         if v is obj:
            nm = k
    print(nm)
It's not working properly in this case:

a=3
b=3
name(a)
The result is
b
(it shoul be a)
I don't get it. First of all why, second of all what (yeah, it's Friday evening in my timezone)

x, y are function parameters/arguments not variable names. What names should be displayed length(2, 3) (assuming that there is no name pointing to values 2 and 3)?

With Python 3.8 anyone can have variable names very easily by using new f-string debugging feature {x=}.

Intended way of using it is something along those lines:

>>> def length(x, y):
...     return f'The length of {x=}, {y=} is {x+y}'
... 
>>> print(length(5, 10))
The length of x=5, y=10 is 15
So one can take advantage of this feature without any function and do something like this:

>>> x, y = 7, 42
>>> name_x_as_string = f'{x=}'.split('=')[0]
>>> name_y_as_string = f'{y=}'.split('=')[0]
>>> f'The length of {name_x_as_string}{name_y_as_string} is {x + y}'
'The length of xy is 49'
Thank you.
I need to think over it.
(Oct-18-2019, 12:29 PM)perfringo Wrote: [ -> ]
>>> x, y = 7, 42
>>> name_x_as_string = f'{x=}'.split('=')[0]
>>> name_y_as_string = f'{y=}'.split('=')[0]
>>> f'The length of {name_x_as_string}{name_y_as_string} is {x + y}'
'The length of xy is 49'

It might be something like this.

But still I need it to be in function. I want to call the function with variables like this:

def length(x,y):
    name_x_as_string = f'{x=}'.split('=')[0]
    name_y_as_string = f'{y=}'.split('=')[0]
    print(f'The length of {name_x_as_string}{name_y_as_string} is {x + y}')

a=5
b=3
length(a,b)
The result here is:

The length of xy is 8
It's not what I need. The result should be:

The length of ab is 8
Is it understandable?
(Oct-20-2019, 01:10 PM)mayadob Wrote: [ -> ]The result should be:

The length of ab is 8

Hi!

I'm not sure what you want. Could it be this???:

def length(x,y):
    print(f"The length of 'ab' is {x + y}.")

a = 5
b = 3
length(a, b)
that produces the following output:
Output:
The length of 'ab' is 8.
All the best,
(Oct-20-2019, 02:01 PM)newbieAuggie2019 Wrote: [ -> ]Hi!

I'm not sure what you want. Could it be this???:

def length(x,y):
    print(f"The length of 'ab' is {x + y}.")

a = 5
b = 3
length(a, b)
that produces the following output:
Output:
The length of 'ab' is 8.
All the best,

What if I call it with:

length(c, d)
length(e,f)
Every time the result should be
The length of 'ab' is ...
While it has to be:

The length of 'cd' is ...
and

The length of 'ef' is ...
As perfringo points out, that's not really possible. Once it's in the function, it's not a and b (or c and d) anymore. It's x and y. If you want the name to be available, you are going to have to store it explicitly. So if a and b are two dimensional points, you might be storing them as tuples: (2, 3) and (4, 5). To get the name out, you need to store it with the tuple. You could do a longer tuple: ('A', 2, 3) and ('B', 4, 5). You could do a dictionary: {'name': 'A', 'coord': (2, 3)} and {'name': 'B', 'coord': (4, 5)}. You could do a named tuple or even a full fledged class. But somehow you need to store the name.
Pages: 1 2