[Tutor] Beginner Question

Albert-Jan Roskam fomcl at yahoo.com
Wed Oct 23 09:58:26 CEST 2013


--------------------------------------------
On Wed, 10/23/13, Steven D'Aprano <steve at pearwood.info> wrote:

 Subject: Re: [Tutor] Beginner Question
 To: tutor at python.org
 Date: Wednesday, October 23, 2013, 5:27 AM
 
 On Tue, Oct 22, 2013 at 04:25:59PM
 +0200, Sven Hennig wrote:
 >  Hello, I would like to learn a programming
 language and have decided to use
 > Python. I have some programming experience and doing
 well in Python. What
 > really causes me problems is OOP.
 > I'm just dont get it... I'm missing a really Practical
 example. In every
 > book I've read are the examples of such Class Dog and
 the function is bark. Has
 > anyone an OOP example for me as it is really used in
 real code, so I can
 > better understand the concept? I do not know why this
 is so hard for me.
 
 I can sympathise. You wouldn't believe how long it took me
 to really 
 grok object-oriented programming. I just didn't get it,
 until I started 
 thinking of OOP as being just syntax for keeping functions
 (called 
 "methods") close to the data they belong with. There is more
 to OOP than 
 that, but that was my first step: OOP helps you keep your
 functions 
 close to the data they work with.
 
<snip>
 
 That's all you need to know to start using object oriented
 programming 
 in Python! Many operations are implemented as methods, for
 instance 
 strings have upper and lower methods, lists have append and
 remove 
 methods, and many more. You'll soon learn the operations
 like len() that 
 aren't methods, but old-school functions.
 
==> This reminded me of a text by Guido van Rossum (I can't find the original page): http://effbot.org/pyfaq/why-does-python-use-methods-for-some-functionality-e-g-list-index-but-functions-for-other-e-g-len-list.htm

So the built-in 'len()' is *really* a function, but calls to  len()  implemented by __len__ are method calls *disguised* as function calls? I sometimes find it easier to write calls to special methods the "normal" way, e.g. instead of "x + y" just write it like "x.__add__(y)" This makes special methods more like other methods and therefore easier to understand, to me at least.

Albert-Jan

PS: sorry about the lack of quoting. Yahoo mail was "upgraded" and now all sorts of stuff stops working, times out, etc. 


More information about the Tutor mailing list