Mailing List Archive

Weak Type Ability for Python
Hi all,
Please make this command for Python (if possible):

>>> x=1
>>> y='a'
>>> wprint (x+y)
>>> 1a

In fact make a new type of print command which can print and show strings
and integers together.

Sincerely yours,
Ali.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On Thu, 13 Apr 2023 at 03:05, Ali Mohseni Roodbari
<ali.mohseniroodbari@gmail.com> wrote:
>
> Hi all,
> Please make this command for Python (if possible):
>
> >>> x=1
> >>> y='a'
> >>> wprint (x+y)
> >>> 1a
>
> In fact make a new type of print command which can print and show strings
> and integers together.
>

Try:

print(x, y)

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 4/12/2023 1:11 PM, Chris Angelico wrote:
> On Thu, 13 Apr 2023 at 03:05, Ali Mohseni Roodbari
> <ali.mohseniroodbari@gmail.com> wrote:
>>
>> Hi all,
>> Please make this command for Python (if possible):
>>
>>>>> x=1
>>>>> y='a'
>>>>> wprint (x+y)
>>>>> 1a
>>
>> In fact make a new type of print command which can print and show strings
>> and integers together.
>>
>
> Try:
>
> print(x, y)
>
> ChrisA

It puts a space between "1" and "a", whereas the question does not want
the space. print(f'{x}{y}') would do it, but only works for variables
named "x" and "y".

As happens so often, the OP has not specified what he actually wants to
do so we can only answer the very specific question.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 4/12/23 11:11, Chris Angelico wrote:
> On Thu, 13 Apr 2023 at 03:05, Ali Mohseni Roodbari
> <ali.mohseniroodbari@gmail.com> wrote:
>>
>> Hi all,
>> Please make this command for Python (if possible):
>>
>>>>> x=1
>>>>> y='a'
>>>>> wprint (x+y)
>>>>> 1a
>>
>> In fact make a new type of print command which can print and show strings
>> and integers together.
>>
>
> Try:
>
> print(x, y)
>
> ChrisA


To continue on, what do you want "addition" of dissimilar types to do -
since that's what you have asked for above.

You can write yourself an object which is happy with certain
combinations, so you don't have this scenario:

>>> x + y
Traceback (most recent call last):
File "<input>", line 1, in <module>
x + y
~~^~~
TypeError: can only concatenate str (not "int") to str
>>> y + x
Traceback (most recent call last):
File "<input>", line 1, in <module>
y + x
~~^~~
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>


Or you can help out the print function by doing some of the fiddling
yourself:

>>> print(f"{x}{y}")
1a




--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 2023-04-12 19:57, Mats Wichmann wrote:
> On 4/12/23 11:11, Chris Angelico wrote:
>> On Thu, 13 Apr 2023 at 03:05, Ali Mohseni Roodbari
>> <ali.mohseniroodbari@gmail.com> wrote:
>>>
>>> Hi all,
>>> Please make this command for Python (if possible):
>>>
>>>>>> x=1
>>>>>> y='a'
>>>>>> wprint (x+y)
>>>>>> 1a
>>>
>>> In fact make a new type of print command which can print and show strings
>>> and integers together.
>>>
>>
>> Try:
>>
>> print(x, y)
>>
>> ChrisA
>
>
> To continue on, what do you want "addition" of dissimilar types to do -
> since that's what you have asked for above.
>
> You can write yourself an object which is happy with certain
> combinations, so you don't have this scenario:
>
> >>> x + y
> Traceback (most recent call last):
> File "<input>", line 1, in <module>
> x + y
> ~~^~~
> TypeError: can only concatenate str (not "int") to str
> >>> y + x
> Traceback (most recent call last):
> File "<input>", line 1, in <module>
> y + x
> ~~^~~
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
> >>>
>
>
> Or you can help out the print function by doing some of the fiddling
> yourself:
>
> >>> print(f"{x}{y}")
> 1a
>
Or:

>>> print(x, y, sep='')
1a
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 2023-04-12 at 14:51:44 -0400,
Thomas Passin <list1@tompassin.net> wrote:

> On 4/12/2023 1:11 PM, Chris Angelico wrote:
> > On Thu, 13 Apr 2023 at 03:05, Ali Mohseni Roodbari
> > <ali.mohseniroodbari@gmail.com> wrote:
> > >
> > > Hi all,
> > > Please make this command for Python (if possible):
> > >
> > > > > > x=1
> > > > > > y='a'
> > > > > > wprint (x+y)
> > > > > > 1a
> > >
> > > In fact make a new type of print command which can print and show strings
> > > and integers together.
> > >
> >
> > Try:
> >
> > print(x, y)
> >
> > ChrisA
>
> It puts a space between "1" and "a", whereas the question does not want the
> space. print(f'{x}{y}') would do it, but only works for variables named "x"
> and "y".

Or possibly print(x, y, sep='').

> As happens so often, the OP has not specified what he actually wants to do
> so we can only answer the very specific question.

Agreed.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 2023-04-12, Ali Mohseni Roodbari <ali.mohseniroodbari@gmail.com> wrote:
> Hi all,
> Please make this command for Python (if possible):
>
>>>> x=1
>>>> y='a'
>>>> wprint (x+y)
>>>> 1a

If that's what you want, use PHP or some other language. Don't try to ruin Python.

> In fact make a new type of print command which can print and show strings
> and integers together.


--
https://mail.python.org/mailman/listinfo/python-list
RE: Weak Type Ability for Python [ In reply to ]
As originally written, the question posed has way too many possible answers
but the subject line may give a hint. Forget printing.

The Python statement
1 + "a"

SHOULD fail. The first is an integer and the second is string. These two
are native Python objects that neither define what to do if they are paired
with an object of the other type on the left or the right.

In any case, what should the answer be? Since "a" has no integer value, it
presumably was intended to be the string "1a".

So why NOT use the built-in conversion and instead of:

print(x+y) # where x=1, y='a'

It should be:

print(str(x) + y)

Could this behavior be added to Python? Sure. I wonder how many would not
like it as it often will be an error not caught!

If you defined your own object derived from string and added a __radd__()
method then the method could be made to accept whatever types you wanted
(such as integer or double or probably anything) and simply have code that
converts it to the str() representation and then concatenates them with, or
if you prefer without, any padding between.

I suspect the OP is thinking of languages like PERL or JAVA which guess for
you and make such conversions when it seems to make sense.

Python does not generally choose that as it is quite easy to use one of so
many methods, and lately an f-string is an easy way as others mentioned.


-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Thomas Passin
Sent: Wednesday, April 12, 2023 2:52 PM
To: python-list@python.org
Subject: Re: Weak Type Ability for Python

On 4/12/2023 1:11 PM, Chris Angelico wrote:
> On Thu, 13 Apr 2023 at 03:05, Ali Mohseni Roodbari
> <ali.mohseniroodbari@gmail.com> wrote:
>>
>> Hi all,
>> Please make this command for Python (if possible):
>>
>>>>> x=1
>>>>> y='a'
>>>>> wprint (x+y)
>>>>> 1a
>>
>> In fact make a new type of print command which can print and show strings
>> and integers together.
>>
>
> Try:
>
> print(x, y)
>
> ChrisA

It puts a space between "1" and "a", whereas the question does not want
the space. print(f'{x}{y}') would do it, but only works for variables
named "x" and "y".

As happens so often, the OP has not specified what he actually wants to
do so we can only answer the very specific question.

--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 12Apr2023 22:12, avi.e.gross@gmail.com <avi.e.gross@gmail.com> wrote:
>I suspect the OP is thinking of languages like PERL or JAVA which guess
>for
>you and make such conversions when it seems to make sense.

JavaScript guesses. What a nightmare. Java acts like Python and will
forbid it on type grounds (at compile time with Java, being staticly
typed).

Cheers,
Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 2023-04-13 03:12, avi.e.gross@gmail.com wrote:
> As originally written, the question posed has way too many possible answers
> but the subject line may give a hint. Forget printing.
>
> The Python statement
> 1 + "a"
>
> SHOULD fail. The first is an integer and the second is string. These two
> are native Python objects that neither define what to do if they are paired
> with an object of the other type on the left or the right.
>
> In any case, what should the answer be? Since "a" has no integer value, it
> presumably was intended to be the string "1a".
>
> So why NOT use the built-in conversion and instead of:
>
> print(x+y) # where x=1, y='a'
>
> It should be:
>
> print(str(x) + y)
>
> Could this behavior be added to Python? Sure. I wonder how many would not
> like it as it often will be an error not caught!
>
> If you defined your own object derived from string and added a __radd__()
> method then the method could be made to accept whatever types you wanted
> (such as integer or double or probably anything) and simply have code that
> converts it to the str() representation and then concatenates them with, or
> if you prefer without, any padding between.
>
> I suspect the OP is thinking of languages like PERL or JAVA which guess for
> you and make such conversions when it seems to make sense.
>
In the case of Perl, there are distinct operators for addition and
string concatenation, with automatic type conversion (non-numeric
strings have a numeric value of 0, which can hide bugs).

> Python does not generally choose that as it is quite easy to use one of so
> many methods, and lately an f-string is an easy way as others mentioned.
>
>
> -----Original Message-----
> From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
> Behalf Of Thomas Passin
> Sent: Wednesday, April 12, 2023 2:52 PM
> To: python-list@python.org
> Subject: Re: Weak Type Ability for Python
>
> On 4/12/2023 1:11 PM, Chris Angelico wrote:
>> On Thu, 13 Apr 2023 at 03:05, Ali Mohseni Roodbari
>> <ali.mohseniroodbari@gmail.com> wrote:
>>>
>>> Hi all,
>>> Please make this command for Python (if possible):
>>>
>>>>>> x=1
>>>>>> y='a'
>>>>>> wprint (x+y)
>>>>>> 1a
>>>
>>> In fact make a new type of print command which can print and show strings
>>> and integers together.
>>>
>>
>> Try:
>>
>> print(x, y)
>>
>> ChrisA
>
> It puts a space between "1" and "a", whereas the question does not want
> the space. print(f'{x}{y}') would do it, but only works for variables
> named "x" and "y".
>
> As happens so often, the OP has not specified what he actually wants to
> do so we can only answer the very specific question.
>

--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 2023-04-13 03:21, Cameron Simpson wrote:
> On 12Apr2023 22:12, avi.e.gross@gmail.com <avi.e.gross@gmail.com> wrote:
>>I suspect the OP is thinking of languages like PERL or JAVA which guess
>>for
>>you and make such conversions when it seems to make sense.
>
> JavaScript guesses. What a nightmare. Java acts like Python and will
> forbid it on type grounds (at compile time with Java, being staticly
> typed).
>
I thought that in Java you can, in fact, concatenate a string and an
int, so I did a quick search online and it appears that you can.
--
https://mail.python.org/mailman/listinfo/python-list
RE: Weak Type Ability for Python [ In reply to ]
On closer reading, the OP may be asking how to make a function doing what
they want, albeit without a plus.

Here is a python function as a one-liner that takes exactly two arguments of
any kind (including string and integer) and concatenates them into one
string without anything between and prints them:

def strprint(first, second): print(str(first) + str(second))

>>> strprint(5,1)
51
>>> strprint("a5",1)
a51
>>> strprint(12,"o'clock")
12o'clock
>>> strprint(3.1415926535,complex(3,4))
3.1415926535(3+4j)

Want something similar for any number of arguments? Here is a slightly
longer one-liner:

def strprintall(*many): print(''.join([str(each) for each in many]))

>>>
strprintall(1,"=egy\n",2,"=kett?\n",3,"=h?rom\n",4,"=n?gy\n",5,"=?t\n","in
my childhood language.\n")
1=egy
2=kett?
3=h?rom
4=n?gy
5=?t
in my childhood language.

Note my meager attempt is not using a plus sign as I addressed a sort of way
that could be done using a __radd__ method or other ways like an f-string.

I can not repeat this often enough. The easiest way to do something you want
in a new language is to work within the existing language as-is and not to
ask the language to change to be the way you want. That can take years or
never happen, and especially if the designers did not want the feature you
ask for.





-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of 2QdxY4RzWzUUiLuE@potatochowder.com
Sent: Wednesday, April 12, 2023 3:17 PM
To: python-list@python.org
Subject: Re: Weak Type Ability for Python

On 2023-04-12 at 14:51:44 -0400,
Thomas Passin <list1@tompassin.net> wrote:

> On 4/12/2023 1:11 PM, Chris Angelico wrote:
> > On Thu, 13 Apr 2023 at 03:05, Ali Mohseni Roodbari
> > <ali.mohseniroodbari@gmail.com> wrote:
> > >
> > > Hi all,
> > > Please make this command for Python (if possible):
> > >
> > > > > > x=1
> > > > > > y='a'
> > > > > > wprint (x+y)
> > > > > > 1a
> > >
> > > In fact make a new type of print command which can print and show
strings
> > > and integers together.
> > >
> >
> > Try:
> >
> > print(x, y)
> >
> > ChrisA
>
> It puts a space between "1" and "a", whereas the question does not want
the
> space. print(f'{x}{y}') would do it, but only works for variables named
"x"
> and "y".

Or possibly print(x, y, sep='').

> As happens so often, the OP has not specified what he actually wants to do
> so we can only answer the very specific question.

Agreed.
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On Thu, 13 Apr 2023 12:21:58 +1000, Cameron Simpson <cs@cskk.id.au>
declaimed the following:

>On 12Apr2023 22:12, avi.e.gross@gmail.com <avi.e.gross@gmail.com> wrote:
>>I suspect the OP is thinking of languages like PERL or JAVA which guess
>>for
>>you and make such conversions when it seems to make sense.
>
>JavaScript guesses. What a nightmare. Java acts like Python and will
>forbid it on type grounds (at compile time with Java, being staticly
>typed).
>

REXX -- where everything is considered a string until it needs to be
something else.

REXX-ooRexx_5.0.0(MT)_64-bit 6.05 23 Dec 2022
rexxtry.rex lets you interactively try REXX statements.
Each string is executed when you hit Enter.
Enter 'call tell' for a description of the features.
Go on - try a few... Enter 'exit' to end.
x = 1;
........................................... rexxtry.rex on WindowsNT
y = "a";
........................................... rexxtry.rex on WindowsNT
say x||y;
1a
........................................... rexxtry.rex on WindowsNT
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 13Apr2023 03:36, MRAB <python@mrabarnett.plus.com> wrote:
>I thought that in Java you can, in fact, concatenate a string and an
>int, so I did a quick search online and it appears that you can.

I stand corrected. I could have sworn it didn't, but it has been a long
time. - Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list
RE: Weak Type Ability for Python [ In reply to ]
Given the significant number of languages other than Python that have some
version of a feature that allows implicit conversion of unlike operands to
concatenate something like a "number" and a string into a string, the
question may not be silly as to how or why Python chose as it chose.

As I see it, python did put in quite a bit of customizability and
flexibility including the ability to create your own object types that alter
the behavior of an operator like plus. I have seen plenty of code that takes
advantage and makes the user of the code assume that different types can
interact seamlessly.

But it is like many other things. Languages that support a wide variety of
integer types such as signed and unsigned versions of integers in 8 bits,
sixteen bits, 32 bits and even 64 bits or perhaps higher, will often look
like they allow you to mix them in various combinations for not just
addition. But underneath it all can be lots of hidden complexity. I have
seen languages that make lots of functions with the same names but different
signatures and then dispatch a call like add(int8, unsignedint32) to the
right function that best matches their signature. The functions internally
can do many things but often convert their arguments to a common format,
perform the operations, then perhaps convert back to whatever result output
was expected.

In the case being discussed we might have to create something that looks
like do_plus(int, int) and then do_plus(int, char) and so on.

The other alternatives can include tables of "generality" and
"convertibility" with rules that govern how to perform a calculation by
changing or upgrading or downgrading things to make a match that can then be
handled.

The issue here is a sort of operator overloading. In Python, "+" does not
mean plus at all. It means whatever the programmer wanted it to mean. An
infix line of code that includes "obj1 + obj2" is supposed to investigate
how to do it. I am not sure if some built-in objects may be different, but
it does a sequence of operations till it finds what it needs and does it.

I believe it looks a bit like this. The LHS object, obj1, is examined to see
if it has defined a __add__() method. If found, it is called and either
produces a result or signals it cannot handle obj2 for some reason. If that
method is not found, or fails, then it looks to see if the RHS, obj2, has a
__radd__() method defined. That can be given obj1 and return an answer or
signal failure.

If obj1 is 5 and obj2 is "pm" then we are using the built-in predefined
classes that currently have no interest in concatenating or adding unlike
types like this. If you could add or change these dunder methods (and note
you may also need to deal with __iadd__() to handle the case where x=5 and
you write "x += "pm" albeit that could weirdly change the type of x without
a linter having a clue.

I suggest the latter argument may be a good enough reason that Python did
not implement this. There are many good reasons. Does anyone like a language
that lets you type 2 + "three" and quietly makes that be 5? Sure, it can be
made to work on a subset of number in say English, but will it work in other
languages. It can be hard enough now to write code in UNICODE (I have seen
some) that tries to determine if a code point represents a number in some
language or representation and treats it as a numeral. I have seen the
numeric bullets such as a dark circle containing a white nine be treated as
if the user had put in a nine, for example.

As was mentioned, some languages have different operators for addition
versus concatenation and in that context, it may be intuitive that using the
wrong object type is an implicit call to conversion. Python uses "+" for
both purposes depending on context and potentially for many more purposes
the programmer can devise.

Consider the asterisk operator as a companion concept. It gladly accepts a
string and a number in either order and does something somewhat intuitive
with them by treating multiplication as a sort of repeated addition:

>>> 5 * "6"
'66666'
>>> "5" * 6
'555555'
>>> 3 * "Hello? "
'Hello? Hello? Hello?

But it will not handle float.

>>> "Hello" * 2.5
TypeError: can't multiply sequence by non-int of type 'float'

If you want to either truncate a float to an into or round it or take a
ceiling, though, it will not guess for you and you must do something
explicit like this:

>>> "Hello" * round(2.5)
'HelloHello'
>>> "Hello" * round(2.6)
'HelloHelloHello'
>>> "Hello" * int(2.6)
'HelloHello'

There is a parallel argument here in arguing it should accept a float and
truncate it. But since you can easily cast a float to an int, in any of many
ways, why have the program choose when it quite likely reflects an error in
the code. And, no, I do not suggest 2.5 be interpreted as putting in an
approximate percentage so that .8 * "Hello" should result in "Hell" ...




-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Cameron Simpson
Sent: Wednesday, April 12, 2023 11:43 PM
To: python-list@python.org
Subject: Re: Weak Type Ability for Python

On 13Apr2023 03:36, MRAB <python@mrabarnett.plus.com> wrote:
>I thought that in Java you can, in fact, concatenate a string and an
>int, so I did a quick search online and it appears that you can.

I stand corrected. I could have sworn it didn't, but it has been a long
time. - Cameron Simpson <cs@cskk.id.au>
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On Thu, 13 Apr 2023 at 15:40, <avi.e.gross@gmail.com> wrote:
> And, no, I do not suggest 2.5 be interpreted as putting in an
> approximate percentage so that .8 * "Hello" should result in "Hell" ...

$ pike
Pike v8.1 release 15 running Hilfe v3.5 (Incremental Pike Frontend)
Ok.
> "Hello, world! " * 2.5;
(1) Result: "Hello, world! Hello, world! Hello, "
> "Hello, world! Hello, world! Hello, " / 10;
(2) Result: ({ /* 3 elements */
"Hello, wor",
"ld! Hello,",
" world! He"
})
> "Hello, world! Hello, world! Hello, " % 10;
(3) Result: "llo, "
> "Hello, world! Hello, world! Hello, " / 10.0;
(4) Result: ({ /* 4 elements */
"Hello, wor",
"ld! Hello,",
" world! He",
"llo, "
})
>

Multiplying and dividing strings by floats makes perfect sense. (The
({ }) notation is Pike's array literal syntax; consider it equivalent
to Python's square brackets for a list.)

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
print(f'{x}{y}') ?

On Wed, Apr 12, 2023 at 7:06?PM Ali Mohseni Roodbari <
ali.mohseniroodbari@gmail.com> wrote:

> Hi all,
> Please make this command for Python (if possible):
>
> >>> x=1
> >>> y='a'
> >>> wprint (x+y)
> >>> 1a
>
> In fact make a new type of print command which can print and show strings
> and integers together.
>
> Sincerely yours,
> Ali.
> --
> https://mail.python.org/mailman/listinfo/python-list
>


--
Personnel et confidentiel
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 4/13/2023 1:38 AM, avi.e.gross@gmail.com wrote:
> In Python, "+" does not
> mean plus at all. It means whatever the programmer wanted it to mean. An
> infix line of code that includes "obj1 + obj2" is supposed to investigate
> how to do it. I am not sure if some built-in objects may be different, but
> it does a sequence of operations till it finds what it needs and does it.

A really nice example of this is pathlib in the standard library. You
can write things like this, overloading the "/" operator:

>>> from pathlib import PurePath
>>> pth = PurePath('c:/') / 'temp' / 'python'
>>> pth
PureWindowsPath('c:/temp/python')
>>> str(pth)
'c:\\temp\\python'

--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 2023-04-13, Cameron Simpson <cs@cskk.id.au> wrote:
> On 12Apr2023 22:12, avi.e.gross@gmail.com <avi.e.gross@gmail.com> wrote:
>
>>I suspect the OP is thinking of languages like PERL or JAVA which guess
>>for you and make such conversions when it seems to make sense.
>
> JavaScript guesses. What a nightmare.

So does PHP. What's really impressive is that it never seems to guess
correctly. :)
--
https://mail.python.org/mailman/listinfo/python-list
RE: Weak Type Ability for Python [ In reply to ]
Chris, I was not suggesting it for Python as one of many possible
implementations.

I do see perfectly valid uses in other contexts. For example, if I have a
program that displays my text as pixels in some font and size, I may indeed
want the text clipped at 2 1/2 repetitions. But as always, when there are
choices to be made, you have to very clearly document the choice or offer
ways to do it another way. In a non-fixed-width font, 2.5 may mean knowing
how many pixels and adjusting so that a narrow letter "i" may be shown in
one font and not another, for example.

If you want completeness, sure, you can define fractional parts of a string
in this context by the percentage of CHARACTERS in it. But as we have often
seen, in other encodings you need to differentiate between varying numbers
of bytes versus the underlying symbols they represent. Your example from
the Pike language not only supports the multiplication of a string but
division and mod. Python currently does not allow those.

So why not extend it to allow complex numbers?

>>> "Hello" * complex(5,0)
TypeError: can't multiply sequence by non-int of type 'complex'
>>> "Hello" * complex(0,5)
TypeError: can't multiply sequence by non-int of type 'complex'

The first one above is actually perfectly valid in the sense that the real
part is 5 and there is no imaginary component. With a bit of effort, I can
use the complexity to work:

>>> "Hello" * int(complex(5,0).real)
'HelloHelloHelloHelloHello'

Let me reiterate. There are languages that do all kinds of interesting
things and some of what Python has done is seen by others as interesting.
They regularly borrow from each other or use parts and innovate further. I
have no serious objection to making well-thought-out changes if they are
determined to be not only useful, but of higher priority than a long
shopping list of other requests. I am wary of overly bloating a language by
placing too many things in the core.

It strikes me as doable to create a module that encapsulates a feature like
this in a limited way. What may be needed is just a carefully constructed
class that starts off as similar to str and adds some methods. Any user
wanting to use the new feature would either start using the new class
directly or cast their str to it when they want it to be useable.

But the good news is that I am nowhere in the python hierarchy and have no
ability to make any changes. This is purely academic for me. And, if I want
such features and see tons of existing ways to get what I want or can roll
it for myself, ...


-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Chris Angelico
Sent: Thursday, April 13, 2023 3:02 AM
To: python-list@python.org
Subject: Re: Weak Type Ability for Python

On Thu, 13 Apr 2023 at 15:40, <avi.e.gross@gmail.com> wrote:
> And, no, I do not suggest 2.5 be interpreted as putting in an
> approximate percentage so that .8 * "Hello" should result in "Hell" ...

$ pike
Pike v8.1 release 15 running Hilfe v3.5 (Incremental Pike Frontend)
Ok.
> "Hello, world! " * 2.5;
(1) Result: "Hello, world! Hello, world! Hello, "
> "Hello, world! Hello, world! Hello, " / 10;
(2) Result: ({ /* 3 elements */
"Hello, wor",
"ld! Hello,",
" world! He"
})
> "Hello, world! Hello, world! Hello, " % 10;
(3) Result: "llo, "
> "Hello, world! Hello, world! Hello, " / 10.0;
(4) Result: ({ /* 4 elements */
"Hello, wor",
"ld! Hello,",
" world! He",
"llo, "
})
>

Multiplying and dividing strings by floats makes perfect sense. (The
({ }) notation is Pike's array literal syntax; consider it equivalent
to Python's square brackets for a list.)

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On Fri, 14 Apr 2023 at 02:05, <avi.e.gross@gmail.com> wrote:
> So why not extend it to allow complex numbers?
>
> >>> "Hello" * complex(5,0)
> TypeError: can't multiply sequence by non-int of type 'complex'
> >>> "Hello" * complex(0,5)
> TypeError: can't multiply sequence by non-int of type 'complex'
>

Clearly a missed opportunity to rotate the text through a specified angle.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 4/12/23 04:03, Ali Mohseni Roodbari wrote:
>
On 4/13/23 07:50, Stefan Ram wrote:
> If tomorrow Python would allow "string+int" and "int+string"
> in the sense of "string+str(int)" and "str(int)+string",
> what harm would be there?
>
> But for now, I think a typical approach would be to just use "str",
> i.e., "string+str(int)" and "str(int)+string".


I agree with Py Zen rule 2 in this case:
Explicit is better than implicit.

I hate when things try to guess what I am doing... It is why I can't use
lxml.
--
https://mail.python.org/mailman/listinfo/python-list
RE: Weak Type Ability for Python [ In reply to ]
[.THIS CLAIMER: a bit off a bit off a bit off topic, imagine that]

Chris,

You have a gift of taking things I think about but censor myself from
including in my post and then blurting it out! LOL!

The original question in this thread now seems a dim memory but we are now
discussing not how to add a number to a string but how to multiply a string
to make n combined copies and then what it means to have a fractional copy
and finally a way to specify a rotation to the result. Argh!!!!

But since you brought it up as a way of looking at what multiplying by an
imaginary number might mean, as in rotating text, I am now going to throw in
a May Tricks even if it is only April.

So should I now extend a language so a rotation matrix is allowed to
multiply text or even a nested list like:

[ [ cos(theta), -sin(theta) ],
[ sin(theta), cos(theta) ]

While we are at it, why stop with imaginary numbers when you can imagine
extensions thereof? Unfortunately, it has been proven there are and can only
be two additional such constructs. Quaternions have three distinct imaginary
axes called i,j,k and some see them as interesting to show multidimensional
objects in all kinds of places such as computer vision or orbital mechanics.
Octonions have seven such other imaginary axes and have uses in esoteric
places like String Theory or Quantum Logic.

And, yes, you can use these critters in python. You can add a quaternion
type to numpy for example. Yep, octonions too. See modules like pyoctonion
and pyquaternion and much more.

The immoral moral of this story is that once you start opening some doors,
you may find people clamoring to let in ever more things and features. You
can easily bog down your code to the point where finding the commonly used
parts becomes a chore as you trudge through lots of code that is rarely used
but there for completeness.

Oh, I want to make something clear before I get another message spelling out
what I was thinking but chose to omit.

I slightly misled you above. Yes, it has been proven no number higher than 8
(meaning one real dimension and seven distinct imaginary ones) can exist so
octonions are the final part of that story. Well, not exactly. You lose
commutativity when going from quaternions to octonions and you lose full
associativity if you go higher. But you can make all kinds of mathematical
constructs like sedenions with 16 dimensions.

I cannot imagine ever trying to multiply a string by these critters but who
knows? As I noted above, if you set some parts of each of the above to zero,
they all can look like something with a real part like 3, and no (meaning
zero point zero) imaginary parts. So you could argue you should support all
kinds of things that MAY on examination turn out to be convertible to an
integer or double.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of Chris Angelico
Sent: Thursday, April 13, 2023 12:12 PM
To: python-list@python.org
Subject: Re: Weak Type Ability for Python

On Fri, 14 Apr 2023 at 02:05, <avi.e.gross@gmail.com> wrote:
> So why not extend it to allow complex numbers?
>
> >>> "Hello" * complex(5,0)
> TypeError: can't multiply sequence by non-int of type 'complex'
> >>> "Hello" * complex(0,5)
> TypeError: can't multiply sequence by non-int of type 'complex'
>

Clearly a missed opportunity to rotate the text through a specified angle.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 2023-04-12 at 22:47:17 -0400,
Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:

> REXX -- where everything is considered a string until it needs to be
> something else.

I see your REXX, and raise you an awk,? except that awk won't add a
string to a number, or a number to string, but it will concatenate in
both cases:

$ echo 1 a | awk '{print $1 $2}{print $1 + $2}'
1a
1

$ echo 1 a | awk '{print $2 $1}{print $2 + $1}'
a1
1

$ echo 1 2 | awk '{print $1 $2}{print $2 + $1}'
12
3

? GNU Awk 5.2.1, API 3.2, PMA Avon 8-g1, (GNU MPFR 4.2.0, GNU MP 6.2.1)

Dan
--
https://mail.python.org/mailman/listinfo/python-list
RE: Weak Type Ability for Python [ In reply to ]
This reminds me a bit of complaints that the parser does not do what you
want when you do not supply parentheses in an expression like:

5 * 4 + 3

In many and maybe most languages it is seen as (5*4)+3 UNLESS you tell it
you want 5*(4+3). There are precedence and associativity rules.

Of course the computer might guess you meant the latter or could refuse to
do it and offer you a choice before calculating it. Or the language may
insist on parentheses always so you would need to also say ((5*4)+3) with no
default behavior.

The golden rule remains. If there is more than one way something can be
done, then either the programmer must make the choice explicit OR the
documentation must very clearly warn which path was chosen and perhaps point
to ways to do other choices.

Some people take more complex (but not Complex) arithmetic than the above
and break it up into quite a few simple parts like:

temp1 = 4 + 3
result = 5 + temp1

Of course, the latter can be hard to read and understand for some people,
and some (others?) find fully parenthesized versions hard. But having
precedence rules and also allowing the other methods, should work fine for a
good segment of people except perhaps the ones who like Reverse Polish
Notation and insist on 5 4 3 + * instead.


-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On
Behalf Of aapost
Sent: Thursday, April 13, 2023 12:28 PM
To: python-list@python.org
Subject: Re: Weak Type Ability for Python

On 4/12/23 04:03, Ali Mohseni Roodbari wrote:
>
On 4/13/23 07:50, Stefan Ram wrote:
> If tomorrow Python would allow "string+int" and "int+string"
> in the sense of "string+str(int)" and "str(int)+string",
> what harm would be there?
>
> But for now, I think a typical approach would be to just use "str",
> i.e., "string+str(int)" and "str(int)+string".


I agree with Py Zen rule 2 in this case:
Explicit is better than implicit.

I hate when things try to guess what I am doing... It is why I can't use
lxml.
--
https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list

1 2  View All