Mailing List Archive

1 2  View All
Re: Weak Type Ability for Python [ In reply to ]
On 4/13/2023 2:36 PM, avi.e.gross@gmail.com wrote:
> 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.

For *reading*, I prefer the usual 5 * (4 + 3) form. But for using a
calculator, I much prefer RPN.

--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On Fri, 14 Apr 2023 at 02:55, <avi.e.gross@gmail.com> wrote:
> And, yes, you can use these critters in python. You can add a quaternion
> type to numpy for example. Yep, octonions too.

Hang on hang on hang on. I can multiply a string by an onion? The
possibilities of script-controlled culinary arts just became that much
more awesome...

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On Fri, 14 Apr 2023 at 03:29, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
>
> 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

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
Re: Weak Type Ability for Python [ In reply to ]
On 4/13/23 13:34, Dennis Lee Bieber wrote:
> 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.

You missed the best REXX, Dennis.

ARexx for the amiga's. Bill Hawes wrote in a link to everything amigados
had and we wrote, in ARexx, stuff that Amigados didn't have starting
with a cron, a better cron than Paul Vixies. Worst, Bill never got a
dime from commode-door for the best language it had, We served our WDTV
web page with it in the middle 90's. Using ARexx and early php. Now its
farmed out and 99% commercial junk.

> x = 1;
> ........................................... rexxtry.rex on WindowsNT
> y = "a";
> ........................................... rexxtry.rex on WindowsNT
> say x||y;
> 1a
> ........................................... rexxtry.rex on WindowsNT

Cheers, Gene Heskett.
--
"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author, 1940)
If we desire respect for the law, we must first make the law respectable.
- Louis D. Brandeis
Genes Web page <http://geneslinuxbox.net:6309/>

--
https://mail.python.org/mailman/listinfo/python-list
RE: Weak Type Ability for Python [ In reply to ]
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@python.org> On
Behalf Of Chris Angelico
Sent: Thursday, April 13, 2023 3:35 PM
To: python-list@python.org
Subject: Re: Weak Type Ability for Python

On Fri, 14 Apr 2023 at 03:29, Dennis Lee Bieber <wlfraed@ix.netcom.com>
wrote:
>
> 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

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

--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 13/04/2023 20:35, Chris Angelico wrote:

> REXX - where everything is a string,

> It was quite the experience back in the day (as OS/2's native
> scripting language),

I briefly met REXX on a mainframe, but I did play with OS/2 for
a year or two. Back when it looked like it might be a rival to M$

OS/2 running NeXTstep now that would have been a platform
for the 90s... both so near yet so far.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


--
https://mail.python.org/mailman/listinfo/python-list
Re: RE: Weak Type Ability for Python [ In reply to ]
On 14/04/2023 00:25, avi.e.gross@gmail.com wrote:

> 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?

Are you talking about something like a C union type (or a Pascal
variant record)? I'm not aware of any such feature in Python but
have often thought it would be a nice to have for the rare cases
where its useful.

> 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.

Or are you meaning something like an extension to the
struct module that can interpret a bytestring in any way
defined by a format string?

> basis. But do some languages have some support within the language itself?

The closest to what you seem to mean is, I think, the C union
type, at least in my experience. But you have to define all
the ways you can interpret the type up front in the type
definition.

> 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.

You can achieve that in a slightly different way in Tcl which
allows you to redefine all the operators (commands in Tcl-speak)
in the language so redefining plus is easy. Doing it based on
type is more tricky but doable.

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

Yes, that's where it becomes a designer's arbitrary choice.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On Fri, 14 Apr 2023 at 09:49, Alan Gauld <learn2program@gmail.com> wrote:
>
> On 13/04/2023 20:35, Chris Angelico wrote:
>
> > REXX - where everything is a string,
>
> > It was quite the experience back in the day (as OS/2's native
> > scripting language),
>
> I briefly met REXX on a mainframe, but I did play with OS/2 for
> a year or two. Back when it looked like it might be a rival to M$
>
> OS/2 running NeXTstep now that would have been a platform
> for the 90s... both so near yet so far.
>

OS/2 was the primary operating system in our house from about 1992ish
until maybe the early 2000s - possibly 2010 even, depending on how you
count things. After a while we needed to permit the occasional Windows
system due to software that didn't work, and then I started deploying
some Linux systems. Eventually the Linux boxes outnumbered the OS/2,
and finally the last OS/2 system was virtualized under Linux,
completing the transition.

But along the way, I spent many years learning the way that OS/2
works, and that's been so valuable to me ever since. The system was
built from the ground up with threads in mind, so every programmer, as
a matter of course, just learned about threads. It's that simple. Want
to make a GUI app? The rule is, you respond to a window message within
a very few milliseconds; if you can't do your processing in that much
time, you spin off a thread. Easy. VX-REXX actually did that for every
message automatically, running thread zero for the system GUI message
loop, and feeding messages to a REXX message loop; you could, of
course, still spawn your own REXX threads as needed.

The entire Presentation Manager and Workplace Shell (broadly
equivalent to a Linux "desktop manager", I think? Kinda?) were object
oriented; you would have a WPDataFile for every, well, data file, but
some of those might be subclasses of WPDataFile. And it was fairly
straight-forward to write your own subclass of WPDataFile, and there
was an API to say "if you would ever create a WPDataFile, instead
create one of my class instead". This brilliant technique allowed
anyone to enhance the desktop in any way, quite impressive especially
for its time. I've yearned for that ever since, in various systems,
although I'm aware that it would make quite a mess of Python if you
could say "class EnhancedInt(int): ..." and then "any time you would
create an int, create an EnhancedInt instead". A bit tricky to
implement.

Ahh, the memories. Clinging onto several old Realtek RTL8029 cards
long past the days when ten megabit networking was considered
adequate, because I knew that I could ALWAYS rely on them. Seriously,
those things just never gave me issues. Problems with networking? Slap
in an 8029 and see if it's the card or something else. Need to
download the driver before your network card works? Grab an 8029, boot
with that, fetch driver, try again. Good times.

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 2023-04-13 08:25:51 -0700, Grant Edwards wrote:
> 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.

Not in this case. Like Perl (Not PERL) it has different operators for
concatenation and addition. So $a + $b is always addition, never
concatenation.

Well, at least numbers and strings. For arrays its a (somewhat bizarre)
union.

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
Re: Weak Type Ability for Python [ In reply to ]
On 4/13/23 7:25 PM, avi.e.gross@gmail.com wrote:
> s 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?

There is nothing that I know of built into Python that does this.

There is no reason you can't write your own class to implement this.
Something that by "default" looks like a string, but in some contexts
(operated on by certain types) sees if its string could be treated as a
value of some other type, and if so, converts its value to that type and
does the operation.

--
Richard Damon

--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 14/04/23 4:55 am, avi.e.gross@gmail.com wrote:
> 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.

You can go beyond that if you broaden your horizons enough.
There are Clifford algebras, Lie algebras, ...

Not sure what any of those should do to strings, though. :-)

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
RE: RE: Weak Type Ability for Python [ In reply to ]
Alan,

Your guess is not quite what I intended.

Something like a C union is just a piece of memory large enough to hold one of several kinds of content and some way to figure out which is currently in place.

I am looking at a data structure that is an object of some class and stores the data in any way that it feels like. But it may be a bit of a chameleon that shows one face or another as needed. I can write code now that simply adds various access methods to the class used and also provides a way to query if it supports some interfaces.

Consider a dumb example. I have an object that holds a temperature and stores it in say degrees Celsius. There are simple formulas that can convert it to Fahrenheit or Kelvin or Rankine. So you can create access methods like get_as_Rankine() but this will only be useful for some programs that know about the interface.

So what if you had a variable in the class such as supported_formats that presented something like a list of scales supported using an official set of names? It may even be possible to get a reference to the function to call to get that functionality, or perhaps you have one access function that accepts any argument on the list and delivers what is wanted.

The temperature would only need to be stored in one format but be available in many. Of course, you could choose to precalculate and store others, or cache them when one request has come in and so forth.

Another example would be dates stored in some format in a class that can deliver the result in all kinds of formats. Yes, we have functions that do things like that. But can you see advantages to the class hiding lots of details internally?

These are just examples but the point is motivated by some interfaces I have seen.

How do you know if something can be used by a context manner such as in a "with" statement? There may be other ways, but it seems two dunder methods, if present, likely mean it is. They are __enter__() and __exit__().

There are other interfaces like for iterators, that sometimes are more complex as when some things are not present, it uses others. Can you have a general function like is_iterator() or is_context_manager() that pretty much guarantees it is safe for the rest of the code to use the object in the way it wants?

My comments about overloading plus were a sort of extra idea. I think we have discussed the general algorithm for how Python tries to resolve something like "obj1 op obj2" and not just for the plus operator. There are quite a few dunder methods that cover many such operators.

What I was thinking about was a bit of a twist on that algorithm. I did something very vaguely like this years ago when I was working on how to translate documents from one format to another, such as WANG, Multimate, Wordperfect, plain text, etc. The goal was for a sender of an email to add an attachment and send it to many people at once. Each recipient would have a known preference for the type of document format they preferred. I wrote an algorithm in C++ which I got early access to as I was working at Bell Labs that effectively used a registered series of translator software along with info on how well or fast they worked, to do as few translations as possible and send each recipient the format they wanted.

Yes, there were major incompatibilities and you sometimes ended up with some features being dropped or changed. But that is not the point. If format A had do direct translator to format Z, I would find the intersection of formats we had software for to translate to from A, and anther set of languages that could be used to translate from to Z. Sometimes it needed multiple hops. It worked fine but never saw the light of day as, weirdly, the project had been canceled months earlier and they were stalling while planning the next project and thus let me do what I wanted even though I was integrating my C++ code into a project that was otherwise al in C.

Now back to Python in this regard. If I type alpha + beta then maybe after trying the methods we have described, if still failing, the algorithm could see if alpha and beta registered what types they could output and see if a match could be made. If a number object offered a string version, that would be a match. If the string offered a numeric version, again problem solved. And even if the match was not precise, sometimes the interpreter might know enough to do a bit more and say convert an integer into a double if the sizes of the contents allowed.

The problem with this, and there are many, is that there is a certain nondeterministic aspect that may cause surprises and plenty of cost.

It was just a academic thought that probably is not needed in the context albeit may be implemented in some projects to bridge things as described or in other novel ways.

-----Original Message-----
From: Alan Gauld <learn2program@gmail.com>
Sent: Thursday, April 13, 2023 8:14 PM
To: avi.e.gross@gmail.com; python-list@python.org
Subject: Re: RE: Weak Type Ability for Python

On 14/04/2023 00:25, avi.e.gross@gmail.com wrote:

> 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?

Are you talking about something like a C union type (or a Pascal
variant record)? I'm not aware of any such feature in Python but
have often thought it would be a nice to have for the rare cases
where its useful.

> 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.

Or are you meaning something like an extension to the
struct module that can interpret a bytestring in any way
defined by a format string?

> basis. But do some languages have some support within the language itself?

The closest to what you seem to mean is, I think, the C union
type, at least in my experience. But you have to define all
the ways you can interpret the type up front in the type
definition.

> 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.

You can achieve that in a slightly different way in Tcl which
allows you to redefine all the operators (commands in Tcl-speak)
in the language so redefining plus is easy. Doing it based on
type is more tricky but doable.

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

Yes, that's where it becomes a designer's arbitrary choice.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



--
https://mail.python.org/mailman/listinfo/python-list
Re: RE: Weak Type Ability for Python [ In reply to ]
On 2023-04-13 at 22:14:25 -0400,
avi.e.gross@gmail.com wrote:

> I am looking at a data structure that is an object of some class and
> stores the data in any way that it feels like. But it may be a bit of
> a chameleon that shows one face or another as needed. I can write code
> now that simply adds various access methods to the class used and also
> provides a way to query if it supports some interfaces.

Python dicts act mostly like hash tables. All by themselves, hash
tables are unordered (and in return for giving up that order, you get
O(1) access to an item if you know its key).

But when you ask a Python dict for the keys, you always get them in the
same order, skipping those that have been deleted since the last time
you asked, and appending the new keys to the end of the list in the
order in which you added them.

There's your chameleon.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On 4/13/2023 10:14 PM, avi.e.gross@gmail.com wrote:
> Alan,
>
> Your guess is not quite what I intended.
>
> Something like a C union is just a piece of memory large enough to hold one of several kinds of content and some way to figure out which is currently in place.
>
> I am looking at a data structure that is an object of some class and stores the data in any way that it feels like. But it may be a bit of a chameleon that shows one face or another as needed. I can write code now that simply adds various access methods to the class used and also provides a way to query if it supports some interfaces.
>
> Consider a dumb example. I have an object that holds a temperature and stores it in say degrees Celsius. There are simple formulas that can convert it to Fahrenheit or Kelvin or Rankine. So you can create access methods like get_as_Rankine() but this will only be useful for some programs that know about the interface.
>
> So what if you had a variable in the class such as supported_formats that presented something like a list of scales supported using an official set of names? It may even be possible to get a reference to the function to call to get that functionality, or perhaps you have one access function that accepts any argument on the list and delivers what is wanted.

Now you are starting to sound like COM. You ask an object for its
IUnknown interface, and from that interface you can query what other
interfaces it has available. You go on from there.
--
https://mail.python.org/mailman/listinfo/python-list
RE: RE: Weak Type Ability for Python [ In reply to ]
Yes, Dave, there are many data structures that can be used to maintain a
list of output types the class claims to support. Dictionaries have the
interesting property that you can presumably have a value that holds a
member function to access the way the key specifies.

Ideally, the order is not important for what I am looking for. Generally, I
would think that any class like the ones I have been discussing, would want
to broadcast a fairly short list of output types it would support.

Of course, if you look at my date example, the list could be quite big but
if you simply use something like strftime() for many of the formats, perhaps
you may not need to list all possible ones. Any valid format could be
accepted as an argument and passed to such a utility function. Your
dictionary might simply store some commonly used formats known to work.

But I repeat. This is not a serious request. I know how to build limited
functionality like this if I ever want it, but wonder if anyone has ever
created a proposal for some protocols and perhaps helpers like say an
embedded object that handles aspects of it once you have initialized your
dictionary and also handles requests to show part of what is stored for any
shoppers wondering if you are compatible with their needs.

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

On 2023-04-13 at 22:14:25 -0400,
avi.e.gross@gmail.com wrote:

> I am looking at a data structure that is an object of some class and
> stores the data in any way that it feels like. But it may be a bit of
> a chameleon that shows one face or another as needed. I can write code
> now that simply adds various access methods to the class used and also
> provides a way to query if it supports some interfaces.

Python dicts act mostly like hash tables. All by themselves, hash
tables are unordered (and in return for giving up that order, you get
O(1) access to an item if you know its key).

But when you ask a Python dict for the keys, you always get them in the
same order, skipping those that have been deleted since the last time
you asked, and appending the new keys to the end of the list in the
order in which you added them.

There's your chameleon.
--
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 05:35:22 +1000, Chris Angelico <rosuav@gmail.com>
declaimed the following:

>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.
>

I still miss the Amiga ARexx implementation which piggy-backed on the
Amiga IPC scheme* such that any application that opened a "RexxPort" could
be the target for "ADDRESS <rexxport>", and hence scripted using ARexx (Did
IBM ever get beyond the two choices of command-line and a line-editor?).


* Granted, that IPC relied upon the fact that all applications shared one
memory space, so there wasn't the overhead of copying data structures from
sending application to the port's linked list and thence to the receiving
application.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On Thu, 13 Apr 2023 20:53:21 -0400, Richard Damon
<Richard@Damon-Family.org> declaimed the following:

>On 4/13/23 7:25 PM, avi.e.gross@gmail.com wrote:
>> s 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?
>
>There is nothing that I know of built into Python that does this.
>
>There is no reason you can't write your own class to implement this.
>Something that by "default" looks like a string, but in some contexts
>(operated on by certain types) sees if its string could be treated as a
>value of some other type, and if so, converts its value to that type and
>does the operation.

I sure don't want to see the documentation for that...

a = thisType(3)
b = thisType(7)
c = 9 #plain integer

print(a + b + c)

(Since I presume left to right evaluation of equal level operations) Does
this result in

46 ("3" + "7" => "37", int("37") + 9 => 46)

or

19 (as int("3") + int("7") => 10, + 9 => 19)

Worse... changing order of a/b/c would make completely different
results...

82 (b + a + c)
127 (int(a) + c returning thisType(12) + b as strings)

and does (c + a) result in returning an integer (a conforming to c); or a
string (a coercing c to thisType).

--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
On Fri, 14 Apr 2023 at 17:17, Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
>
> On Fri, 14 Apr 2023 05:35:22 +1000, Chris Angelico <rosuav@gmail.com>
> declaimed the following:
>
> >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.
> >
>
> I still miss the Amiga ARexx implementation which piggy-backed on the
> Amiga IPC scheme* such that any application that opened a "RexxPort" could
> be the target for "ADDRESS <rexxport>", and hence scripted using ARexx (Did
> IBM ever get beyond the two choices of command-line and a line-editor?).
>
>
> * Granted, that IPC relied upon the fact that all applications shared one
> memory space, so there wasn't the overhead of copying data structures from
> sending application to the port's linked list and thence to the receiving
> application.

Yeah, the "ADDRESS" command has so much potential. Back in the day, I
built a MUD with REXX scripting, and within those scripts, the default
ADDRESS target was "send message to the invoker of the command", so
you could do something like this:

/* A bare string gets sent to the client */
"Hello!"
/* You can still invoke shell commands, if you actually need to */
address cmd "do_a_thing"

Rabid proponents of OOP say that it's all about sending messages to
things. Well, the ADDRESS command truly lets you send messages, so....
that means REXX is the most object oriented language there is, right?

ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: Weak Type Ability for Python [ In reply to ]
{one more entry in the thread drift, and then I think I'll shut up}

On Fri, 14 Apr 2023 17:22:47 +1000, Chris Angelico <rosuav@gmail.com>
declaimed the following:


>Yeah, the "ADDRESS" command has so much potential. Back in the day, I
>built a MUD with REXX scripting, and within those scripts, the default
>ADDRESS target was "send message to the invoker of the command", so
>you could do something like this:

I used to think (Open)VMS would have been a potential platform for an
effective ADDRESS command. Where ARexx used a shared memory linked-list for
the "port" (as I'd stated, avoiding copying of data), (Open)VMS had
"mailboxes" (they would have to copy data -- or at best remap the virtual
memory from one process to another) which could serve the same purpose;
receiving application creates/owns the named mailbox, sending applications
would provide the data packet including a return mailbox link.

>
>/* A bare string gets sent to the client */
>"Hello!"
>/* You can still invoke shell commands, if you actually need to */
>address cmd "do_a_thing"
>
Yeah, that was the other feature -- anything that could not be parsed
as a REXX statement was automatically sent to whatever the current ADDRESS
host happened to be.
--
https://mail.python.org/mailman/listinfo/python-list
RE: Weak Type Ability for Python [ In reply to ]
Dennis,

Before I reply, let me reiterate I am NOT making a concrete suggestion, just having a somewhat abstract discussion.

The general topic is a sort of polymorphism I envisioned where a select group of classes/objects that can be seen as different aspects of an elephant can be handled to provide some functionality in a consistent way. We all agree much of the functionality can be done deliberately by individual programmers. The question was whether anyone had done a more general implementation or even saw any reason to do so.

Fair enough?

So let us assume I have an object, call it obj1, that encapsulates data the old fashioned way. Consider a classical case like an object holding information about a parallelopiped or something like a shoebox. How you store the info can vary, such as recording a height/width/depth, or a series of x,y,z coordinates representing some of the vertices. But no matter how you store the basic info, you can derive many things from them when asked to provide a volume or surface area or whether it will fit within another object of the same kind assuming the sides have no width. Or, you can ask it to return another instance object that has double the width or many other things.

There are several ways to provide the functionality, actually quite a few, but one is to make a method for each thing it does such as obj1.get_surface_area(), obj1.get_volume() and obj1.does_it_fit_in(cl2) and of course you can have methods that change the orientation or ask what angles it is oriented at now and whatever else you want.

Each such method will return something of a usually deterministic type. Volumes will be a double, for example. But what if you design a small language so you can type obj1.get_by_name("volume") and similar requests, or even a comma separated grouping of requests that returns a list of the answers? It now is not so deterministic-looking to a linter. But normal Python allows and often encourages such polymorphism so is this anything new?

What I envisioned is a tad closer to the latter. Not this:

a = thisType(3)
b = thisType(7)
c = 9 #plain integer
print(a + b + c)

Note the above example is standard. My thoughts are a bit more arcane and focused on convertibility of a single value into multiple forms.

Say I have a data type that stores a number representing a temperature. It may have ways to initialize (or change) the temperature so it can be input as degrees centigrade or Fahrenheit or Kelvin or Rankine or even more indirect ways such as 10 degrees Fahrenheit above the freezing point of pure water at a particular atmospheric pressure and so on.

What I want to add is a bit like this. Internally many methods may get created that may not be expected to be used except through a designated interface. Call them f1() and f2() ... fn() for now.

Also in the class initialization or perhaps in the object dunder init, you create something like a dictionary consisting of key words matched by pointers to the aforementioned functions/methods. This structure will have some name designated by the protocol such as _VIEWS and may be visible to anyone looking at the object. The details can be worked out but this is a simplistic explanation.

In this dictionary we may have words like "Celsius", "Fahrenheit" and so on, perhaps even several variants that point to the same functions. If a user wants the temperature in absolute terms, they may call a standard function like "obj1.as_type('Kelvin')" and that function will search the dictionary and get you the results using the appropriate conversion method. You may also support other accessors like 'obj1.supports_type("Fahrenheit451")' that reports as True/False whether the object can handle that output. It merely checks the internal dictionary. You may have another that returns a list of the data types as keys and whatever else is part of the design.

You can, of course, have a second set of such directives that instead of returning a temperature as a double, will return a printable text version that includes ? or °K or °R or °F".

A second example could be something holding a date with plenty of internal abilities to display it in a wide variety of formats or maybe just holds a day of the week that it will display as a string in any language it handles, such as Sunday being shown as:
???? ????????
Diman?o
Vasárnap
Sonntag
Dimanche
Zondag
???
??????

And so on. Again, it need not store the text for every language but can call translation software as needed and it can be more than the name of a day of the week. It could have a dictionary containing all the languages it handles as described for another example and access methods. Of course, if called on repeatedly and often for the same languages, it could cache results.

My question, again, is not whether this can be done but whether some kind of protocol can be created that is published and suggests the names and so on to use in constructing your own implementation. And, it may be helpful if a module is made available that makes it even simpler to use.

I think it is time for me to drop out of this discussion unless I hear something of interest. It is just an idea I have been pondering.

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of Dennis Lee Bieber
Sent: Friday, April 14, 2023 12:23 AM
To: python-list@python.org
Subject: Re: Weak Type Ability for Python

On Thu, 13 Apr 2023 20:53:21 -0400, Richard Damon
<Richard@Damon-Family.org> declaimed the following:

>On 4/13/23 7:25 PM, avi.e.gross@gmail.com wrote:
>> s 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?
>
>There is nothing that I know of built into Python that does this.
>
>There is no reason you can't write your own class to implement this.
>Something that by "default" looks like a string, but in some contexts
>(operated on by certain types) sees if its string could be treated as a
>value of some other type, and if so, converts its value to that type and
>does the operation.

I sure don't want to see the documentation for that...

a = thisType(3)
b = thisType(7)
c = 9 #plain integer

print(a + b + c)

(Since I presume left to right evaluation of equal level operations) Does
this result in

46 ("3" + "7" => "37", int("37") + 9 => 46)

or

19 (as int("3") + int("7") => 10, + 9 => 19)

Worse... changing order of a/b/c would make completely different
results...

82 (b + a + c)
127 (int(a) + c returning thisType(12) + b as strings)

and does (c + a) result in returning an integer (a conforming to c); or a
string (a coercing c to thisType).

--
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-14 10:19:03 +1000, Chris Angelico wrote:
> The entire Presentation Manager and Workplace Shell (broadly
> equivalent to a Linux "desktop manager", I think? Kinda?) were object
> oriented; you would have a WPDataFile for every, well, data file, but
> some of those might be subclasses of WPDataFile. And it was fairly
> straight-forward to write your own subclass of WPDataFile, and there
> was an API to say "if you would ever create a WPDataFile, instead
> create one of my class instead". This brilliant technique allowed
> anyone to enhance the desktop in any way, quite impressive especially
> for its time. I've yearned for that ever since, in various systems,
> although I'm aware that it would make quite a mess of Python if you
> could say "class EnhancedInt(int): ..." and then "any time you would
> create an int, create an EnhancedInt instead". A bit tricky to
> implement.

Or alternatively you might be able to add or replace methods on the
existing int class. So 5 is still just an int, but now (5 + "x") calls
the modified __add__ method which knows how add a string to an int.

Might make even more of a mess ;-).

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
Re: Weak Type Ability for Python [ In reply to ]
On 2023-04-13 03:28:37 +0100, MRAB wrote:
> On 2023-04-13 03:12, 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.
> >
> 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).

You get a warning for that, though.

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

1 2  View All