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.
x=1
y='a'
wprint (x+y)
1a
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
Traceback (most recent call last):x + y
Traceback (most recent call last):y + x
1aprint(f"{x}{y}")
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
1aprint(x, y, sep='')
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
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.
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.
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
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.
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 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.
51strprint(5,1)
a51strprint("a5",1)
12o'clockstrprint(12,"o'clock")
3.1415926535(3+4j)strprint(3.1415926535,complex(3,4))
strprintall(1,"=egy\n",2,"=kettõ\n",3,"=három\n",4,"=négy\n",5,"=öt\n","in
On 4/12/2023 1:11 PM, Chris Angelico wrote:strings
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
and integers together.
theTry:
print(x, y)
ChrisA
It puts a space between "1" and "a", whereas the question does not want
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.
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).
On 12Apr2023 22:12, avi.e...@gmail.com <avi.e...@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
On 2023-04-13 03:21, Cameron Simpson 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 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.
'66666'5 * "6"
'555555'"5" * 6
'Hello? Hello? Hello?3 * "Hello? "
TypeError: can't multiply sequence by non-int of type 'float'"Hello" * 2.5
'HelloHello'"Hello" * round(2.5)
'HelloHelloHello'"Hello" * round(2.6)
'HelloHello'"Hello" * int(2.6)
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.
And, no, I do not suggest 2.5 be interpreted as putting in an
approximate percentage so that .8 * "Hello" should result in "Hell" ...
"Hello, world! " * 2.5;(1) Result: "Hello, world! Hello, world! Hello, "
"Hello, world! Hello, world! Hello, " / 10;(2) Result: ({ /* 3 elements */
"Hello, world! Hello, world! Hello, " % 10;(3) Result: "llo, "
"Hello, world! Hello, world! Hello, " / 10.0;(4) Result: ({ /* 4 elements */
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
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.
PureWindowsPath('c:/temp/python')from pathlib import PurePath
pth = PurePath('c:/') / 'temp' / 'python'
pth
'c:\\temp\\python'str(pth)
As happens so often, the OP has not specified what he actually wants to
do so we can only answer the very specific question.
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.
TypeError: can't multiply sequence by non-int of type 'complex'"Hello" * complex(5,0)
TypeError: can't multiply sequence by non-int of type 'complex'"Hello" * complex(0,5)
'HelloHelloHelloHelloHello'"Hello" * int(complex(5,0).real)
And, no, I do not suggest 2.5 be interpreted as putting in an
approximate percentage so that .8 * "Hello" should result in "Hell" ...
"Hello, world! " * 2.5;(1) Result: "Hello, world! Hello, world! Hello, "
"Hello, world! Hello, world! Hello, " / 10;(2) Result: ({ /* 3 elements */
"Hello, world! Hello, world! Hello, " % 10;(3) Result: "llo, "
"Hello, world! Hello, world! Hello, " / 10.0;(4) Result: ({ /* 4 elements */
So why not extend it to allow complex numbers?
TypeError: can't multiply sequence by non-int of type 'complex'"Hello" * complex(5,0)
TypeError: can't multiply sequence by non-int of type 'complex'"Hello" * complex(0,5)
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".
So why not extend it to allow complex numbers?
TypeError: can't multiply sequence by non-int of type 'complex'"Hello" * complex(5,0)
TypeError: can't multiply sequence by non-int of type 'complex'"Hello" * complex(0,5)
REXX -- where everything is considered a string until it needs to be something else.
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.
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".
And, yes, you can use these critters in python. You can add a quaternion
type to numpy for example. Yep, octonions too.
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
[THIS CLAIMER: a bit off a bit off a bit off topic, imagine that]
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.
The immoral moral of this story is that once you start opening some doors,
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
Can I bring a part of this discussion a bit closer to Python?
Unfortunately, if they BOTH are flexible, how do you decide whether
to add them as numbers or concatenate them as strings?
REXX - where everything is a string,
It was quite the experience back in the day (as OS/2's native
scripting language),
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 tobe
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
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?
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.
basis. But do some languages have some support within the language itself?
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.
Unfortunately, if they BOTH are flexible, how do you decide whether to add them as numbers or concatenate them as strings?
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.
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.
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?
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.
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?
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.
basis. But do some languages have some support within the language itself?
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.
Unfortunately, if they BOTH are flexible, how do you decide whether to add them as numbers or concatenate them as strings?
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.
Alan,methods to the class used and also provides a way to query if it supports some interfaces.
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
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 thiswill 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.
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.
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.
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.
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.
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?
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"
On Friday, 14 April 2023 at 09:23:25 UTC+2, Chris Angelico wrote:
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?
No, that eventually is event-driven, while
OO essentially is about encapsulation (of
data and its operations: inheritance and
polymorphism come later).
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.
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 thefunctionality 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?
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).
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.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 447 |
Nodes: | 16 (2 / 14) |
Uptime: | 24:26:42 |
Calls: | 9,235 |
Calls today: | 2 |
Files: | 13,496 |
Messages: | 6,063,717 |