Python Forum
Inconsistency in Python programming language?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inconsistency in Python programming language?
#1
Hi!

Well, this is a question that arose in some other threads that I was participating in, and I wanted to give it a bit more of a thought, and to ask for some insights from others.

The process of converting one data-type into another data-type is called casting, so in theory, with the str(), float(), and int() functions, we can transform a numeric value string into a float or into an integer, and in a similar way, a float into a string or an integer, or an integer into a string or a float.

So now, I'm going to show the inputs and outputs of some of these transformations:
Output:
>>> str(4) '4' >>> str('4') '4' >>> str(4.5) '4.5' >>> str('4.5') '4.5' >>> float(4) 4.0 >>> float('4') 4.0 >>> float(4.5) 4.5 >>> float('4.5') 4.5 >>> int(4) 4 >>> int('4') 4 >>> int(4.5) 4 >>> int('4.5')
Error:
Traceback (most recent call last): File "<pyshell#16>", line 1, in <module> int('4.5') ValueError: invalid literal for int() with base 10: '4.5'
where we can see that:

1) a numeric value, naked or wrapped in quotes, in the format of a whole number or in the format of a float, can be transformed into a string with no problems.

2) a numeric value, naked or wrapped in quotes, in the format of a whole number or in the format of a float, can be transformed into a float with no problems.

3) a numeric value, naked or wrapped in quotes, in the format of a whole number or in the format of a float, can be transformed into an integer with no problems, EXCEPT WHEN IT'S A FLOAT WRAPPED IN QUOTES, THAT PRODUCES AN ERROR.


It seems that this is a contradiction on the casting of data types, or an inconsistency in Python programming language itself.

Do you have an explanation for that behaviour?
All insights are welcome!!!

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#2
In your opinion what integer should come out of '4.5'? i.e. what would be the correct one?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Oct-04-2019, 05:48 AM)buran Wrote: In your opinion what integer should come out of '4.5'? i.e. what would be the correct one?

Thanks for participating in my dilemma.

I think that following the consistency of these transformations:
Output:
>>> str(4) '4' >>> str('4') '4' >>> str(4.5) '4.5' >>> str('4.5') '4.5' >>> float(4) 4.0 >>> float('4') 4.0 >>> float(4.5) 4.5 >>> float('4.5') 4.5 >>> int(4) 4 >>> int('4') 4 >>> int(4.5) 4
the logical output would be:
Output:
>>> int('4.5') 4
because otherwise it seems that it would be a contradiction on the casting of data types, and wouldn't be consistent. Huh

I'm probably wrong, but I cannot stop thinking that it seems a contradiction and an inconsistency. Confused

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#4
Use one week JavaScript and you'll love the type safety in Python.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
I guess one way to think about it is that you can't convert a string to an int by the int() function if the string is not an int.

In Python it appears (I am still learning so don't take as gospel) that casting and conversion are synonyms, but that is not always true. In other languages casting may be an assertion where conversion takes action. In those cases casting an object to a more specific object gives you the ability to act on it differently, but does not necessarily result in a conversion of that object to the more specific object. Forgive me, I babble.
Reply
#6
(Oct-04-2019, 06:18 AM)newbieAuggie2019 Wrote: it seems a contradiction and an inconsistency

The following approach could cover all cases (e.g where we might not be sure whether the input string has decimal):

int(float("4.5"))
Output:
4
A.D.Tejpal
Reply
#7
(Oct-04-2019, 07:24 AM)DeaD_EyE Wrote: Use one week JavaScript and you'll love the type safety in Python.

(Oct-04-2019, 12:07 PM)jefsummers Wrote: In other languages casting may be an assertion where conversion takes action. In those cases casting an object to a more specific object gives you the ability to act on it differently, but does not necessarily result in a conversion of that object to the more specific object.
Sorry, I'm a newbie at Python. I'm afraid I have no further knowledge about other programming languages than when I look for some doubts in Python, and I find that in some cases, they explain how the same is done in Java or in C. While at times I can guess the similarities and differences, in a similar way as I can somewhat manage to change a program written in Python 2 into Python 3, without having studied Python 2, I couldn't do much more than that.

(Oct-04-2019, 02:55 PM)adt Wrote: The following approach could cover all cases (e.g where we might not be sure whether the input string has decimal):

int(float("4.5"))
Output:
4
I like that, although of course it is just a way to bypass the problem, and not a direct approach to handle what it seems a contradiction and an inconsistency. I guess it is something that only people belonging to the Python Software Foundation could address in the case they were inclined to ...

Thank you everyone for your insights!!!

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#8
It's not an inconsistency. Just because you think it should work a particular way and it doesn't do that consistently doesn't make it inconsistent. It is entirely possible that it doesn't work the way you think it should. You can easily define the intent of those functions in ways that make the observed results consistent. I'm not going to pick one and argue for it, because I'm not Guido and I didn't design Python, so I don't really know. I'm just happy that Guido did design Python, and if parts of it seem odd from my perspective, then I will just roll with it. Because it's not a big deal.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(Oct-04-2019, 05:19 PM)ichabod801 Wrote: It's not an inconsistency. Just because you think it should work a particular way and it doesn't do that consistently doesn't make it inconsistent.
I understand that and as I said,
(Oct-04-2019, 06:18 AM)newbieAuggie2019 Wrote: I'm probably wrong [ ... ]

(Oct-04-2019, 05:19 PM)ichabod801 Wrote: I'm not going to pick one and argue for it [ ... ]
It was not my intention to argue at all. Sorry if my intentions were taken wrongly. I just wanted to see other points of view on this particular topic. I apologise again if my post was thought of as a wish to get into an argument. Pray

(Oct-04-2019, 05:19 PM)ichabod801 Wrote: I'm just happy that Guido did design Python, and if parts of it seem odd from my perspective, then I will just roll with it. Because it's not a big deal.
I completely agree, and I don't think that finding something that I see as an oddity in python, would make me love less python.

I apologise again if somebody felt offended for my post.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#10
(Oct-04-2019, 06:59 PM)newbieAuggie2019 Wrote: I just wanted to see other points of view on this particular topic.
When i first encountered this i just Google'd how to do what i intended, and did it. I always have my personal way, and have to change it based on others. My wife can attest to that.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Programming robots using Python OscarBoots 5 3,459 Oct-31-2021, 09:38 AM
Last Post: Larz60+
  logo language code into python aayushi98 2 67,130 Jan-26-2021, 09:02 PM
Last Post: Serafim
  Programming Difficult math in Python Huntern 6 4,765 Oct-17-2019, 06:32 AM
Last Post: Huntern
  Terms describing Python Programming language leodavinci1990 3 2,729 Aug-12-2019, 02:48 PM
Last Post: leodavinci1990
  Please help a newbie choose which programming language to learn. yeto 2 3,501 Feb-25-2019, 12:56 AM
Last Post: yeto
  Python Programming Projects for Beginners jack_sparrow007 3 3,329 Dec-26-2018, 07:52 PM
Last Post: micseydel
  How to get image from WolframAlpha by using Python language manhnt 1 2,694 Oct-27-2018, 02:07 PM
Last Post: Larz60+
  Programming Python as a MS Windows app? Brian123 8 4,247 Oct-17-2018, 10:26 PM
Last Post: Brian123
  Help with Python programming mediaos 5 3,755 Aug-08-2018, 01:02 PM
Last Post: Larz60+
  How to make the python default language be 3.6 instead of 2.7 sylas 4 6,834 Jul-06-2018, 06:11 AM
Last Post: sylas

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020