tag:blogger.com,1999:blog-8699431508730375743.post5526235131217291440..comments2016-07-20T07:15:28.987-07:00Comments on The History of Python: The story of None, True and False (and an explanation of literals, keywords and builtins thrown in)Guido van Rossumhttp://www.blogger.com/profile/12821714508588242516noreply@blogger.comBlogger16125tag:blogger.com,1999:blog-8699431508730375743.post-6595291638627010992014-08-06T10:32:21.853-07:002014-08-06T10:32:21.853-07:00I'd expect that any function that returns eith...I'd expect that any function that returns either a (possibly-empty) iterable or None presumably means something by returning None—e.g., it's distinguishing between "your query returned no values from the table" and "the table doesn't exist to query".<br /><br />So, if I write code that calls such a function and wants to ignore the important distinction it's making and treat the two the same way, it's probably a good thing that I have to do so explicitly—e.g., it if it is not None else (), or a try/except.<br /><br />Of course if the library is badly-designed and makes a distinction for no reason… well, that's no different from any other badly-designed library; the ideal solution is to fix it upstream or wrap it in a sensible API, but for quick&dirty use, the fact that I have to make my workarounds explicit is again probably more of a useful signal to the reader than a waste of his attention.barnerthttps://www.blogger.com/profile/07390113222049541617noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-55491533598701554172014-03-18T10:27:51.725-07:002014-03-18T10:27:51.725-07:00To Jason's question, I wonder how the BDFL wou...To Jason's question, I wonder how the BDFL would view:<br /><br />for x in None or (): print(x)<br /><br />as a workaround.Aaron Hallhttps://www.blogger.com/profile/04750451514384953771noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-89389636816425053922013-11-20T07:25:55.213-08:002013-11-20T07:25:55.213-08:00Jason, it seems wrong that a function could either...Jason, it seems wrong that a function could either return an iterable, e.g. list, or None. Why would it not return an empty list, tuple, etc? A function returning None to indicate none probably returns a non-iterable for non-none.Ralph Corderoyhttps://www.blogger.com/profile/13140975971019765573noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-78908339443097616762013-11-20T06:52:45.523-08:002013-11-20T06:52:45.523-08:00Tangental question (maybe better asked at StackOve...Tangental question (maybe better asked at StackOverflow):<br /><br />I was quiet surprised that:<br /><br />for x in None :<br /> print x<br /><br />raised: TypeError: 'NoneType' object is not iterable<br /><br />My (naive) expectation was the loop would work like 'for x in () :' aka pass right over nothing.<br /><br />Why would I do this? Well it's quite common (AFAICT) to return None in functions. It's also common to iterate over the results of a function. It'd be 'nice' if I could:<br /><br />for x in function_possibly_returning_None :<br /> print x<br /><br />rather than <br /><br />myiterable = function_possibly_returning_None<br />if myiterable :<br /> for x in myiterable :<br /> print x<br />Jasonhttps://www.blogger.com/profile/18040872212781301984noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-6157924597055939462013-11-18T20:54:55.579-08:002013-11-18T20:54:55.579-08:00Very educational indeed, thanks Guido.Very educational indeed, thanks Guido.Gathogohttps://www.blogger.com/profile/14124702144506645078noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-70349432721588719102013-11-15T20:32:57.535-08:002013-11-15T20:32:57.535-08:00Brilliant! Brilliant! abdulapopoolahttps://www.blogger.com/profile/16731604352756053121noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-15555919394719378132013-11-12T03:30:50.947-08:002013-11-12T03:30:50.947-08:00Well, clearly I didn't think this through. I m...Well, clearly I didn't think this through. I mistakenly mixed up variables and objects... Thanks for clarifying.RB14https://www.blogger.com/profile/11991922948769076950noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-54250128811316146572013-11-11T16:34:14.817-08:002013-11-11T16:34:14.817-08:00@RB14: It's not the same. When you reassign e....@RB14: It's not the same. When you reassign e.g. 'str', you are messing only with your *own* code (the scope of the assignment). When you redefine the built-in itself, you are messing with *everyone's* code.Guido van Rossumhttps://www.blogger.com/profile/12821714508588242516noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-20350633632717478182013-11-11T12:25:42.425-08:002013-11-11T12:25:42.425-08:00Some time ago I asked @ Stackoverflow why Python&#...Some time ago I asked @ Stackoverflow why Python's built-ins methods can't be overridden or why can't you add new methods to built-ins?<br /><br />Best answer I got was that if it were allowed, libraries that use these built-ins might give bad results because it breaks the assumptions those libraries made about the built-ins. But since you say yourself that built-ins variables such as int, str... CAN be overridden, I see no reason why they can't be just extended with new methods, or override their default behaviour. Further more, I believe that libraries (external modules), have their own namespace and once a function is defined inside a module to use a certain object, it can't be overridden from another module:<br /><br />module1.py:<br />print(int)<br /><br />module2.py:<br />int = 5<br />import module1<br /><br />This will print "", (which is the int class string representation), and not "5".<br /><br />Here is my question @ SO (it starts with a different question in mind, but very soon I got to the point I made here):<br />http://stackoverflow.com/questions/19083160/can-literals-in-python-be-overridden<br /><br />Guido, I'd love to get your statement on that matter.RB14https://www.blogger.com/profile/11991922948769076950noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-54307875167970557252013-11-11T12:15:16.427-08:002013-11-11T12:15:16.427-08:00It isn't a literal, but unlike all the others ...It isn't a literal, but unlike all the others is valid.Tom Kenthttps://www.blogger.com/profile/00443552278549079958noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-54924410393290268922013-11-11T12:13:31.586-08:002013-11-11T12:13:31.586-08:00Small typo:
>>> x = 0xfffe
>>> x...Small typo:<br />>>> x = 0xfffe<br />>>> x<br />65534<br />>>> x = 0xfffe+10<br />>>> x<br />65544<br />>>><br />Tom Kenthttps://www.blogger.com/profile/00443552278549079958noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-28590397652150198532013-11-11T05:36:59.186-08:002013-11-11T05:36:59.186-08:00Isn't there an inherent gain for C code that a...Isn't there an inherent gain for C code that also needs to deal with these values?patryshttps://www.blogger.com/profile/12826298786973085887noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-10816508702994258482013-11-11T04:49:47.058-08:002013-11-11T04:49:47.058-08:00Having "credits", etc., be variables has...Having "credits", etc., be variables has always seemed very icky to me. The REPL could have recognised invalid Python as its own commands to provide access to them, similar to SQL REPLs; few expect «python -c 'print credits'» to work.<br /><br />Worse, http://bugs.python.org/issue8220 shows how exit() exists without importing sys. I've had others use site.py's exit thinking they were using sys's.Ralph Corderoyhttps://www.blogger.com/profile/13140975971019765573noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-25348200187401906402013-11-11T01:13:28.321-08:002013-11-11T01:13:28.321-08:00@Bryan Chen:
That is definitely not the moral of t...@Bryan Chen:<br />That is definitely not the moral of this story. The moral is that languages and their use evolve over time and therefore it is good practice to design and maintain your language to allow for change/improvement.<br /> <br />In retrospect True/False could have been keywords. By using keywords sparingly and incorporating mechanisms like __future__, the designers left room to fix this.Snirphttps://www.blogger.com/profile/03522173226688201303noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-73415233845646113802013-11-10T19:34:06.078-08:002013-11-10T19:34:06.078-08:00Moral of the story: When first designing a new lan...Moral of the story: When first designing a new language, always make True/False as keywords.Bryan Chenhttps://www.blogger.com/profile/02740978942945469849noreply@blogger.comtag:blogger.com,1999:blog-8699431508730375743.post-15037068902054955012013-11-10T14:48:30.962-08:002013-11-10T14:48:30.962-08:00It is false that this post has not been educationa...It is false that this post has not been educational. <br />(Forgive me, couldn't resist.)Stevehttps://www.blogger.com/profile/00852288711042186044noreply@blogger.com