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 <learn2prog...@gmail.com> 
Sent: Thursday, April 13, 2023 8:14 PM
To: avi.e.gr...@gmail.com; python-list@python.org
Subject: Re: RE: Weak Type Ability for Python

On 14/04/2023 00:25, avi.e.gr...@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

Reply via email to