Weak Type Ability for Python

avi.e.gross at gmail.com avi.e.gross at gmail.com
Thu Apr 13 19:25:45 EDT 2023


Can I bring a part of this discussion a bit closer to Python?

I stipulate that quite a few languages, including fairly early ones, treated
text often as numbers. Ultimately, much of programming is about taking in
text and often segregating parts into various buckets either explicitly or
by checking if it has a decimal point or looks like scientific notation or
in how it seems to be used.

Is there any concept in Python of storing information in some way, such as
text, and implementing various ideas or interfaces so that you can query if
the contents are willing and able to be viewed in one of many other ways?

As an example, an object may store a fairly large number as a text string in
decimal format. The number is big enough that it cannot be fully represented
in an 8 bit storage as in a byte or in a signed or unsigned 16-bit integer
but can be stored in something larger. It may be possible to store it in a
double precision floating point but not smaller. Yes, I know Python by
default uses indefinite length integers, but you get the idea.

Or it may be storing text in some format but the object is willing to
transform the text into one of several other formats when needed. The text
may also have attributes such as whether it is in English or Hungarian or is
mixed-language.

So for some applications, perhaps leaving the object as a string all the
time may be reasonable. If some operation wishes to use the contents, the
interface can be queried to see what other formats it can be coerced into
and perhaps a specified set of methods need to be included that perform your
transition for you such as object.return_as_int64() 

This can have wider implications. Imagine an object holding text in French
that has been tested by humans using a program like Google Translate and
deemed reasonable for translating into a specific dozen languages such as
Esperanto  and not certified into others like Klingon or High Valyrian or
ASL. The object could contain interfaces for the languages it supports but
does not store the translations, especially when the content is dynamic,
such as a form letter that has been instantiated with a name and address and
perhaps a part detailing what is being billed or shipped. Instead, the
object can present an interface that lets a user determine if it supports
dynamic translation to one or more target, such as the Quebec version of
French or a British versus American version of English.

I am being quite general here and lots of programs out there already
probably have their own way of providing such facilities on a case-by-case
basis. But do some languages have some support within the language itself?

I do note some languages allow objects to oddly belong to multiple classes
at once and have used that feature as a way to check if an object has some
capabilities. Some languages have concepts like a mix-in and Python does
allow thing like multiple inheritance albeit it often is best not to use it
much. It does have ideas about how to test if a class implements some things
by seeing if various dunder methods are in place.

My reason for asking, is based on the discussion. If I want to use plus with
an integer and a string, it may be reasonable for the interpreter to ask one
or the other operand if they are able to be seen another way. If an integer
indicates it can be seen as text, great. If a string indicates it believes
it can deliver a number, great.

Unfortunately, if they BOTH are flexible, how do you decide whether to add
them as numbers or concatenate them as strings?

Sigh!


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

On Fri, 14 Apr 2023 at 03:29, Dennis Lee Bieber <wlfraed at ix.netcom.com>
wrote:
>
> On Thu, 13 Apr 2023 12:21:58 +1000, Cameron Simpson <cs at cskk.id.au>
> declaimed the following:
>
> >On 12Apr2023 22:12, avi.e.gross at gmail.com <avi.e.gross at 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

REXX - where everything is a string, arithmetic can be done on
strings, and data structures are done in the variable name instead of
the value.

I've seen quite a few strings-only languages, but I can't think of any
other language than REXX where you create arrays and dictionaries by
using computed variable names.

It was quite the experience back in the day (as OS/2's native
scripting language), and one that I'm truly glad to have had, as it
taught me so much about the differences between languages.

(It also taught me to treasure good documentation and value it as
truly precious, because SysSetObjectData was an incredibly powerful
function whose docs just referred to WinSetObjectData, and the window
manager's docs weren't part of what I had available.)

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



More information about the Python-list mailing list