On Nov 18, 2019, at 06:02, [email protected] wrote:
>
> x = 5
> y = 5
> print (x+y) #prints 10
> This is easy because everyone knows that + sign adds 2 values together.
>
> print(str(x)+str(y)) #prints 55
> I understand that there are more then one way to do this but, the bottom line
> is, this is not logical.
Sure it is. You’re concatenating two strings, using the same operator used to
concatenate two lists or two tuples. It’s only misleading because those two
strings happen to be digits.
If the operator were &, it would be far more misleading that 5&5 is 5 (everyone
knows that & intersects two values, which means bitwise and for integers) but
str(5)&str(5) was "55" rather than “5”. Because now, you’re using the same
operator used to intersect two sets or two third-party bitfield objects etc.,
but using it to concatenate two strings.
> Tony+Maria
> is what we used to write when we were little children fallen in love, but
> once we grow up, we started to think (more or less) and write the following:
> Tony & Maria
Your argument is that + is simple enough that it’s the first thing little
children think of, but it’s too complicated for Python developers to learn?
> This is pretty much what i suggest. Add new COMBINE operator sign & that will
> combine what ever value there is into one printable line without need for
> converting or formatting it.
So you can’t use & on ints, sets, etc. anymore? Or would there be some magic
where it only does the convert and concatenate for types that don’t define &?
(That would be even more confusing.)
> Examples:
> x = ("I love")
> y = ("this idea ")
> z = ("posted on November ")
> a = 18
> print (x & y & z & a) # prints I love this idea posted on November 18
If you just want this for printing, why not just use separate arguments?
print(x, y, z, a)
The print function calls str on all of its arguments and prints them. By
default it prints them with spaces in between (so you don’t need the awkward
extra space before the close quote in your x, y, and z, which you missed at
least one of in your examples), but if you don’t want that, you can use sep=''.
In addition to being more flexible, and already working, it’s also shorter, and
more readable (the commas don’t compete for attention with the things you’re
actually trying to print the way the ampersands do).
> As of right now, this could look like this:
> x = ("I love")
> y = ("this idea ")
> z = ("posted on November ")
> a = 18
> print (x + y + z + str(a))
> and thats not much difference to worry about. Problem comes when there are
> more different data types need to be added and combined together.
Why? Every data type is converted to string in the same way: the str function.
Every data type is also automatically handled by print. So it doesn’t matter
how many different data types you throw in, it’s not going to get more
complicated than integers.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/TDTUVMZ3OZZDZOPI54UK42KNUGCHJPOA/
Code of Conduct: http://python.org/psf/codeofconduct/