Posts: 6
Threads: 1
Joined: Oct 2019
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)
Posts: 6
Threads: 1
Joined: Oct 2019
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
Posts: 12,032
Threads: 486
Joined: Sep 2016
length is len, not lenght
rather than 'something like' make an effort and actually try to get some working code.
Posts: 6
Threads: 1
Joined: Oct 2019
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)
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
Posts: 6
Threads: 1
Joined: Oct 2019
Thank you.
I need to think over it.
Posts: 6
Threads: 1
Joined: Oct 2019
(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?
Posts: 212
Threads: 25
Joined: Aug 2019
Oct-20-2019, 02:01 PM
(This post was last modified: Oct-20-2019, 02:01 PM by newbieAuggie2019.)
(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
Posts: 6
Threads: 1
Joined: Oct 2019
(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 ...
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
|