Weak Type Ability for Python

Dennis Lee Bieber wlfraed at ix.netcom.com
Fri Apr 14 00:22:43 EDT 2023


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

>On 4/13/23 7:25 PM, avi.e.gross at 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).
			


More information about the Python-list mailing list