[Tutor] Not Really Questions
G'day, While the list is kind of slow I thought I'd post a few thoughts on a couple of things in Python that bug me. They're not really questions but maybe someone can help me understand. The first one is lists... I can't for the life of me understand why a list starts at zero. In everything else in life other than programming the 1st item in a list is always 1. The next thing I don't understand is why the last number in a range is not used... For a in range(1,6): print a, 1 2 3 4 5 Once again it defies the logic of everything else we are taught in life. The 3rd whinge is object oriented programming. I think I understand the principle behind OOP but in practise, to me, it just makes programs jumbled, unreadable and bloated. Just about every summary I have read on Python says it is designed to have a simple syntax and is easy to learn. As a beginner I can look at Python code and have a very good idea of what is happening and why unless it's written in OOP style in which case I have no idea. John _ Read, write and reply to Hotmail on your mobile. Find out more. http://mobilecentral.ninemsn.com.au/mcmobileHotmail/home.aspx ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Not Really Questions
Let me see if I can tackle these.. On Jun 4, 2006, at 8:33 AM, John Connors wrote: > The first one is lists... I can't for the life of me understand why a > list > starts at zero. In everything else in life other than programming the > 1st > item in a list is always 1. > > The next thing I don't understand is why the last number in a range is > not > used... > Once again it defies the logic of everything else we are taught in > life. This is basically a historical precedent. At one point, long long ago, lists were implemented as a sequence of bytes in memory. The "list" was really the address of the first byte. So the index meant "look this many bytes after the first byte". The first byte was zero bytes after the first byte, so its index was zero. The range thing is similar -- think of the arguments to range being (first index to include, first index to exclude). There have been a few programming languages that have changed this (Pascal comes to mind), but none have fared well. You'll get used to it :) > The 3rd whinge is object oriented programming. I think I understand the > principle behind OOP but in practise, to me, it just makes programs > jumbled, > unreadable and bloated. Just about every summary I have read on Python > says > it is designed to have a simple syntax and is easy to learn. As a > beginner I > can look at Python code and have a very good idea of what is happening > and > why unless it's written in OOP style in which case I have no idea. I will be one of the first to say that OOP is grossly overused in commercial programming -- at some point, middle-management learned that "OOP is good", and demanded that programmers start using it everywhere. Sometimes it's the right tool for the job, and sometimes it isn't. I won't get into the details of that distinction, except to say that simpler tasks tend to not require OOP, while more complex tasks often do. Thus, the non-OOP programs you're looking at are probably performing simpler tasks than the OOP programs. Again, you'll get used to it. One of the nice things about Python is that it lets you write both OOP and non-OOP programs, so you've got a lot of room to experiment and learn OOP style. Java, on the other hand, requires OOP for everything, making it much harder to learn when OOP is and is not appropriate. Dustin -- # Dustin J. Mitchell # [EMAIL PROTECTED]/[EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Not Really Questions
John Connors wrote: G'day, While the list is kind of slow I thought I'd post a few thoughts on a couple of things in Python that bug me. They're not really questions but maybe someone can help me understand. The first one is lists... I can't for the life of me understand why a list starts at zero. In everything else in life other than programming the 1st item in a list is always 1. The next thing I don't understand is why the last number in a range is not used... For a in range(1,6): print a, 1 2 3 4 5 Once again it defies the logic of everything else we are taught in life. The 3rd whinge is object oriented programming. I think I understand the principle behind OOP but in practise, to me, it just makes programs jumbled, unreadable and bloated. Just about every summary I have read on Python says it is designed to have a simple syntax and is easy to learn. As a beginner I can look at Python code and have a very good idea of what is happening and why unless it's written in OOP style in which case I have no idea. Dustin pretty well covered it. I'll add: OOP is never necessary. Alan Turing proved that with this theoretial "Turing Machine". Anything that can be computed can be computed on a turing machine. BUT doing so can be very difficult to program. So programming languages with their underlying compilers, assemblers, translators, runtime engines, etc. were invented to make programming "easier". For me OOP is very helpful to separate data from logic. When I find myself writing if elif else sequences to distinguish between 2 similar but not identical collections of data, I (almost) always ask "could this be a reason for 1 or more subclasses"? Also if I have multiple instances of the same thing it is a lot easier for me to track the individual data values in class instances rather than in some sequence or mapping (the alternative if one does not use classes). Welcome to the adventure. You are standing -- Bob Gailer 510-978-4454 Broadband Phone Service for local and long distance $19.95/mo plus 1 mo Free ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Not Really Questions
John Connors wrote: > G'day, > > While the list is kind of slow I thought I'd post a few thoughts on a couple > of things in Python that bug me. They're not really questions but maybe > someone can help me understand. Maybe I can give you some not-really answers ;) > > The first one is lists... I can't for the life of me understand why a list > starts at zero. In everything else in life other than programming the 1st > item in a list is always 1. Many programming languages start indexes at zero. It makes sense if you think of a list being stored as sequential values in memory, and the index as the offset from the start of the list. (This is in fact how arrays are often implemented.) > > The next thing I don't understand is why the last number in a range is not > used... > > For a in range(1,6): > print a, > > 1 2 3 4 5 > > Once again it defies the logic of everything else we are taught in life. This actually prevents many types of errors. Alex Martelli has a good explanation here (also see his other posts in the same thread): http://groups.google.com/group/comp.lang.python/msg/579b53de640190cd > > The 3rd whinge is object oriented programming. I think I understand the > principle behind OOP but in practise, to me, it just makes programs jumbled, > unreadable and bloated. Just about every summary I have read on Python says > it is designed to have a simple syntax and is easy to learn. As a beginner I > can look at Python code and have a very good idea of what is happening and > why unless it's written in OOP style in which case I have no idea. OOP does take some getting used to, it is a different way of thinking about the structure of a program. Done badly, it is a good way to make a mess. Done well, it is a tool for creating a highly modular program where the responsibilities of each piece are well defined. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Not Really Questions
John Connors wrote: > > The first one is lists... I can't for the life of me understand why a list > starts at zero. In everything else in life other than programming the 1st > item in a list is always 1. Hi, Exactly, everything else other than programming. Zero indexed arrays are the norm in everything but moronic old VB. I guess it's just a defacto standard now. > > The next thing I don't understand is why the last number in a range is not > used... > > For a in range(1,6): > print a, > > 1 2 3 4 5 > This relates to the previous issue. This comes from the fact that range(3) = [0, 1, 2] This is extremely useful for iterating indices, and I can suppose that when range() was first extended, it had to remain consistent. > The 3rd whinge is object oriented programming. I think I understand the > principle behind OOP but in practise, to me, it just makes programs jumbled, > unreadable and bloated. Just about every summary I have read on Python says > it is designed to have a simple syntax and is easy to learn. As a beginner I > can look at Python code and have a very good idea of what is happening and > why unless it's written in OOP style in which case I have no idea. > OOP is a rare beast to me: it makes suitable problems very very easy (think about GUI programming without OOP) and unsuitable problems extremely convoluted. I guess it's just the fact that it is a paradigm and not just a programming technique. Fortunately, unless other languages which *force* you to use OOP (think Java), Python allows you to use at least 3 different paradigms (OOP, functional(like Lisp et al) and structured(like Pascal and C)) Just my 2 cents, Hugo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Not Really Questions
Hi John, I'll pitch in although I've read most of the other answers too so I'll be adding to them mostly. > The first one is lists... I can't for the life of me understand why > a list > starts at zero. In everything else in life other than programming Not quite. In math zero is usually the starting point, its generally viewed as a positive number(although it is obviously neither positive or negative) and proofs and definitions usually start by consideriung zero - or at least defining whether zero is in or out of scope. This math point is important because programming was originally a branch of math and it was mathematicians who laid the ground rules. But, it was by no means cast in stone until implementations of list indexing started to be based on the start addressof the list in memory. In C this is even explicitly visible. We can declare anm array of numbers like this int anArray[] = {1,3,5}; and then a pointer to an integer like this int* intPtr; We can now set intPtr to point at the array: intPtr = anArray; If we printed thevcontents of intPtr we would get 1 Now increment the pointer: intPtr == 1 Now id we print iojntPtr we will get 5, the next value in the array. We can also get the 3rd element by using intPtr = anArray + 2 printf("%d",*intPtr) // print the content of intPtr and of course we get the same result with printf("%d",intArray[2]) // print the 3rd element of intArray So in C you can see that indexing is really a very thin wrapper around memory aroithmetic. > The next thing I don't understand is why the last number in a range > is not > used... > > For a in range(1,6): >print a, > > 1 2 3 4 5 This is to make range match with the indexing. We can do stuff like for index in range(len(alist)): print alist[index] If the two did not match you would do something like: for index in range(len(alist)): print alist[index-1] If you are going to be weird at least be consistently weird! Finally some languages allow us to use "normal"! indexing Pascal for example allows: program test(output) var anArray : array [1..5] of integer; n : integer; begin for n := 1 to 5 do begin anArray[n] = 2 * n end; writeln anArray[1]; writeln anArray[3]; writeln anArray[5] end. Which populates an array with indices from 1 through 5 > The 3rd whinge is object oriented programming. I think I understand > the > principle behind OOP but in practise, to me, it just makes programs > jumbled, Further to the previous comments, OOP has several purposes and is used to achieve difgfferent things. But fundamentally OOP is used to control the complexity of large programs. If you are writing program of less than 100 lines you almost never need to use OOP (You may choose to for some of the other reasons below, but its not needed). Between 100 and 1000 lines you are more likely to find OOP coming in handy. Between 1000 lines and 10,000 lines you will almost certainly find at least some of your code that benefits. Beyond 10,000 lines (and I'm, talking Python code here) OOP will be almost indispensible in helping you keep the structure of the code clear in your mind (and in your code too!) There are other ways to control complexity and very large programs have been written without it. It fact its fairly safe to say that all the biggest programs are OOP free being written in COBOL. But COBOL has its own mechanisms for managing complexity and there is now an object oriented COBOL available to combine the best of both worlds. [On a personal note the two biggest programs I've worked on have both been COBOL - 10 million and 6 million lines respectively. The 3rd biggest was C++ at 3.5 million lines. The COBOL was easier... But I've also worked on a 500K lines C program(with no OOP) and that was the hardest of all of them! So OOP works but its not the holy grail that some pundits would have you believe] Other reasons to use OOP are that it definitely makes reuse easier both within a project and between projects. It also can reduce the code size on some types of project. It definitely makes code more flexible by allowing objects to be swapped in and out if they have the same "interface" or protocol. Also some people find objects more easy to think about. It seems more logical to move the table than to change the table's x and y coordinates... However folks brought up with a math or science background, or who have already programmed find it very hard to make the mental switch to seeing the world as communicating objects rather than data being maniupulated by functions... And thats way more than I set out to write!!! HTH -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Not Really Questions
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Alan Gauld wrote, On 06/04/2006 04:01 PM: > Hi John, > > I'll pitch in although I've read most of the other answers > too so I'll be adding to them mostly. > >> The first one is lists... I can't for the life of me understand why >> a list >> starts at zero. In everything else in life other than programming > > Not quite. In math zero is usually the starting point, its generally > viewed as a positive number(although it is obviously neither positive > or negative) and proofs and definitions usually start by consideriung > zero - or at least defining whether zero is in or out of scope. That is just not true. A number is positive if and only if it is strictly greater than 0 by definition. Zero is not considered positive in mathematics. In fact, the set of positive integers, Z+, is the set {1,2,3,}. - -- Yi Qiang ([EMAIL PROTECTED]) Web: http://www.yiqiang.net Tel: (206) 661-8469 PGP: 0xB515564B -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFEg4w9tXlIMrUVVksRAn9RAJ9KAuaKvNvkXFwPNSn0+tMCKwOWMQCdHx6j L8pOPQRJMv8GcnypMblCIgI= =dtF3 -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Not Really Questions
* Hugo González Monteverde <[EMAIL PROTECTED]> [060604 13:04]: > > Exactly, everything else other than programming. Zero indexed arrays are > the norm in everything but moronic old VB. I guess it's just a defacto > standard now. I make equal parts of my income from writing python code and from writing code in rebol. Rebol starts indexes with 1. It also uses the 'first function (like lisp). This has prompted me to write and use the following: def first(v): return v[0] (-: Regardless of where you start counting, there you are! > OOP is a rare beast to me: it makes suitable problems very very easy > (think about GUI programming without OOP) and unsuitable problems > extremely convoluted. I guess it's just the fact that it is a paradigm > and not just a programming technique. comparing rebol and python - rebol is more productive, line for line, from my experience. But because of python's OOP engineering it, it scales better, enabling more maintainable code in larger projects. Consequently, I use python for big projects, rebol for small. """ The right tool for the right job """ tim -- Tim Johnson <[EMAIL PROTECTED]> http://www.alaska-internet-solutions.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Not Really Questions
I think we should say that the number set is zero through nine (not 1-10 as we were taught in school), making "zero" the first number in the set; thus the offset by one. Of course zero is not a number, but a placeholder for a number. Thankfully this concept was invented a few centuries ago in India and brought to the West by Arab scholars. Unfortunately I think it's still not widely understood. __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Not Really Questions
hi john, everyone else has posted great replies to your inquiries, so i'll keep mine brief. > While the list is kind of slow I thought I'd post a few thoughts on a couple > of things in Python that bug me. my 1st comment is that you are not talking about Python alone. everything you state pertains to most other programming languages. > The first one is lists... I can't for the life of me understand why a list > starts at zero. In everything else in life other than programming the 1st > item in a list is always 1. sequences, arrays, etc., in most other languages do start at zero, and have a strong tie to that addressing stuff that's already been mentioned. this is just a fact of programming. > The next thing I don't understand is why the last number in a range is not > used... > > For a in range(1,6): > print a, > > 1 2 3 4 5 > > Once again it defies the logic of everything else we are taught in life. this is partially due to your 1st issue, that things start at zero. a sequence of length 5, for example, or range(5) means: 0, 1, 2, 3, 4. even tho the counting is up to 4, you will see that this is a collection of five items. this feature of "i up-to-but-not-including j" also mirrors other languages, say counting in C for example: for (i = 0; i < 5; i++); > The 3rd whinge is object oriented programming. like all programming tools, one must investigate whether it is the right tool for the job. if it's a simple one-off script, then probly not; but if it is a huge framework where you want to most code reuse, least amount of code to maintain, minimization of the number of bugs, and being able to lump functionality/logic with data, OOP is the way to go. a simple example would be shapes and functions like finding an area. like objects can share the same functions (called "methods"), i.e. square.area(), rectangle.area(), whereas for other objects that aren't similar, i wouldn't want the wrong functionality but i *would* want to use the same name, circle.area(). the good news, as others have mentioned, is that Python is flexible, letting you build the application as you see fit, and not locked to any specific paradigm except perhaps KISS. good luck! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] problem with unpickling an object
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi list, I am pickling a dictionary that has as one of it's values an object I create on the fly. When I try to unpickle that object, cPickle attempts to recreate that object but of course that module is not present anymore. How can I just make it skip over that particular key and just set the value to None? Thanks, - -- Yi Qiang ([EMAIL PROTECTED]) Web: http://www.yiqiang.net Tel: (206) 661-8469 PGP: 0xB515564B -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFEg8k0tXlIMrUVVksRAt2tAJ4hq/3KmDo6BbS3p3AwhKf4Ga52DQCfXFtx GERJqs3IcLXuKaRcq/zuRUE= =SEZ6 -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Not Really Questions
>> Not quite. In math zero is usually the starting point, its >> generally >> viewed as a positive number(although it is obviously neither >> positive >> or negative) > That is just not true. A number is positive if and only if it is > strictly greater than 0 by definition. Zero is not considered > positive > in mathematics. In fact, the set of positive integers, Z+, is the > set > {1,2,3,}. Quite so (as I pointed out in parens) but I'll retract the comment about it being viewed as positive and say it is viewed as being not negative (which is of course true!). Thus zero is frequently treated as the first significant number when considering sequences and ranges of numbers (Z+ not withstanding! :-) The introduction of zero into math was a huge step and as a result a lot of classical math does not consider it but in practical day to day math and science zero is treated as the first number. That's all I was really trying to say... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor