Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print variable name?
#1
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)
Reply
#2
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
Reply
#3
length is len, not lenght
rather than 'something like' make an effort and actually try to get some working code.
Reply
#4
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)
Reply
#5
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'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Thank you.
I need to think over it.
Reply
#7
(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?
Reply
#8
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#9
(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 ...
Reply
#10
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print variable without '' and spaces arnonim 1 722 Jan-30-2023, 05:23 PM
Last Post: deanhystad
  Problem with print variable in print.cell (fpdf) muconi 0 655 Dec-25-2022, 02:24 PM
Last Post: muconi
  Remove a space between a string and variable in print sie 5 1,776 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Can you print a string variable to printer hammer 2 1,936 Apr-30-2022, 11:48 PM
Last Post: hammer
  Print variable between " paulo79 5 1,693 Mar-11-2022, 05:16 PM
Last Post: deanhystad
  When I print a key from dict it prints it but when I try to assign it to a variable I stefanvelikov 3 2,352 Nov-27-2020, 01:29 PM
Last Post: stefanvelikov
  Print variable values from a list of variables xnightwingx 3 2,617 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Print part of a variable rs74 4 2,572 Jul-27-2020, 04:39 PM
Last Post: rs74
  How do I print a returned variable calculated in another function? RedSkeleton007 3 3,527 Jul-10-2018, 12:10 PM
Last Post: buran
  Empty variable when using print before readline fstefanov 3 3,653 Oct-23-2017, 02:22 AM
Last Post: fstefanov

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020