PEP 238 - non-integer division
Sorry for jumping in so late. I'd like to argue in opposition to 238. 1) pep 238 introduces significant backwards incompatibility. Existing code may work differently, or may even fail. Consider x/2&1. If x/2 is automatically promoted to a float as in pep 238, you'll get an error: "TypeError: unsupported operand type(s) for &". If you want to write code that will work now and in the future, you'll need to do: int(x/2)&1 which is tedious and error prone. 2) pep 238 breaks consistency with C, C++ and Java. Considering that, like C++ and Java, Python incorporated most of C's operators including all of the integer operators (<<, &, |, ^, etc.), having one operator behave differently will impede transfer of knowledge to/from those languages. This is especially troublesome since python is so often used in a hybrid C or Java environment. 3) pep 238 makes the language inconsistent with itself. None of the other operators return a float for integer arguments. In general terms, there's an automatic promotion from int to long int to float. 4) The rationale given for pep 238 is that 1/2==0 is a "hump in the learning curve" and "manages to trip up new programmers regularly." This may be the case, but it's not clear that pep 238 reduces the learning curve. I think it just moves the hump a little farther down the road and possibly makes it bigger at the same time. Consider trying to teach type promotion. If you now have to include "except for '/'" in every explanation, it will be a much harder concept to grasp and apply. As long as Python treats integers differently than other numbers (e.g. doesn't allow the use of certain operators like '&'), users will need to understand that there is a difference between integers and floats and what the ramifications are. -- Comparisons with Perl have been mentioned. In Perl 1/2==0.5. However, what Perl does is _radically_ different from what is being proposed. Perl operates on "numbers" which in most cases behave like floats. Internally, ints are used where possible for efficiency, but as far as I can tell, there's no way to determine whether an integer or a float (with an integer value) is being stored, and it doesn't matter. Perl just jumps back and forth between integer and float (and even string) automatically, always trying to "do the right thing." Perl _almost_ always does what you want. However, it often feels a little bit too magical, that it's hard to develop an intuition for what's going on under the hood. If Python could be made to work like Perl and work on "numbers" without making users worry about ints vs floats, this would seem to be a much better approach. However, it seems like much too fundamental a change to be making at this stage of the development of the language. Brent
At 11:38 AM 7/26/2001 -0700, Brent Burley wrote:
Sorry for jumping in so late. I'd like to argue in opposition to 238.
Yes, your points are being represented vociferously on comp.lang.python. The thing about / is it's exceptional in mathematics itself. *, + and - are closed for integers, but / takes you out into the rationals (as per how these keys work on any calculator -- except we use floats). a/b will be essentially equivalent to float(a)/b a//b will be divmod(a,b)[0] It's true that code will break, but you have the option to specify which version of Python you app needs. If the app is being actively maintained, then you can keep abreast the latest version. My understanding is the change will be finalized in 3.x, which was slated to be not-so-backward-compatible in any case. Kirby
languages. This is especially troublesome since python is so often used in a hybrid C or Java environment.
To argue the other side, C and Java both require you to explicity declare the type of your variable, and the rules of coercian tell you, in the code itself, when a change is happening. But in Python, I have much less to go on when trying to track types, especially when user input is considered. So there's something to be said for forcing a type where you might be in doubt given lack of knowledge about the args. Given Python is not C or Java in design, especially with regard to types, it makes sense that it behave differently if it needs to (it should be true to itself, not to C or Java). In today's heterogenous programming environment, you have very different animals working together in hybrid situations. They shouldn't be forced to pay attention to how the others work, as long as they're consistent internally. Guido shouldn't have to turn to C or Java for guidance, and pro programmers should give up the idea that these two are to be regarded as hegemonic. Kirby
At 12:24 PM 7/26/2001 -0700, Kirby Urner wrote:
languages. This is especially troublesome since python is so often used in a hybrid C or Java environment.
To argue the other side, C and Java both require you to explicity declare the type of your variable, and the rules of coercian tell you, in the code itself, when a change is happening. But in Python, I have much less to go on when trying to track types, especially when user input is considered. So there's something to be said for forcing a type where you might be in doubt given lack of knowledge about the args.
Last point, then I'll shut up re this thread: you might counter that by my argument a + b is as potentially ambiguous as a/b, as we don't necessarily know the types of a, b in either case, going in. That's true, but the core point to realize is that where ints and floats are concerned, a + b doesn't lose info (longs are another story). The problem with / is that it might lose info without telling us. b*(a/b) might not give us something close to a, whereas b+(a-b) always will return a (if a,b = int and/or floats). So the point is, when pushing ambiguous types through a potentially lossy operator, do we want to have an ambiguous or unambiguous result? The proposed design change opts for the latter. It's a reality check to make up for the weak typing of variables, the difficulty of tracking, lexically, the types of args involved. I think Arthur points too much emphasis on someone writing their own programs and following the unambiguous rules in the tutorial, vs. someone inheriting code written by others, and trying to puzzle out what's going on. The pro programmer (paid) is as likely to be in the latter situation as the former. It's the readability of code that we're after, long after the original coders are out of the picture, not just how easy it is to dash off something and have it do what you want (that's programming for self alone as reader, and is the hallmark of non-professional coding). Kirby
participants (2)
-
Brent Burley
-
Kirby Urner