Re: Basic inheritance question



Lie a écrit :
On Jan 7, 2:46 am, Bruno Desthuilliers
<bdesth.quelquech...@xxxxxxxxxxxxxxxxxxx> wrote:
Lie a écrit :

On Jan 5, 5:40 pm, MartinRineh...@xxxxxxxxx wrote:
Jeroen Ruigrok van der Werven wrote:
Shouldn't this be:
self.startLoc = start
self.stopLoc = stop
Thanks! Of course it should. Old Java habits die slowly.
No, seriously it isn't Java habits only, most other languages wouldn't
need explicit calling of class name.
Where is the "explicit calling of class name" exactly ?

Perhaps I was a bit tired when writing that (I wouldn't understand
what I wrote if I were you)... what I meant is most other languages
doesn't usually enforce us to explicitly state the containing class
name, which in python is generally called "self".

'self' (or whatever you name it) is not the "containing class name", it's the first argument of the function - which usually happens to be the current instance when the function is used as a method.

Most other languages
1) automatically assign the containing class' object

s/containing class' object/current instance/

in a keyword
(Java: this, VB: Me) behind the screen,

That's not very far from what a Python method object does - automatically assign the current instance to something. The difference is that Python uses functions to implement methods (instead of having two distinct contructs), so the only reliable way to "inject" the reference to the current instance is to pass it as an argument to the function (instead of making it pop from pure air).

There are some benefits to this solution. One of them is the ability to dynamically assign functions as methods. So if you do have some function taking an object as first argument, you can easily turn it into a method.

and 2) automatically searches
variable name in both the local variable table and the containing
class variable table (so to refer to a class variable named var from a
method inside the class, we only need to write var, not self.var as in
python).

This - as you know - cannot work well with Python's scoping rules and dynamicity. Anyway, implicit object reference is definitively a BadThing(tm) wrt/ readbility, specially with multiparadigm languages (like Python or C++). Why do you think soooo many C++ shops impose the m_something naming scheme ?

Anyway, I actually know 3 languages (4 if C# works the same) that has this implicit 'this' (or whatever the name) 'feature', and at least 5 that don't. So I'm not sure that the "most other languages" qualifier really applies to point 2 !-)

In VB, Me is extremely rarely used,

I used to systematically use it - like I've always systematically used 'this' in C++ and Java.

in Python, self is all
over the place. Well, there is positive and negative to both sides,
convenience in VB, and flexibility in Python.

As far as I'm concerned, there's *no* positive point in implicit object reference, and there has never been (and before some paranoid nutcase around accuse me of overzealous biggotry : I already held this very same opinion years before I discovered Python).

Compare the following codes:
VB.NET:
Public Class A
Dim var
Public Function aFunction()
return var

Add three levels of inheritence and a couple globals and you'll find out that readability count !-)

In any non-trivial piece of C++ code, and unless the author either used the explicit 'this' reference or the 'm_xxx' naming convention, you'll have hard time figuring out where a given name comes from when browsing a function's code.

.



Relevant Pages

  • Re: Class Variable Access and Assignment
    ... functions in other languages, but what is the common way of referring to a class variable. ... the <var> defined in the class where a method you define now is implemented, or you might want in the class of the instance object. ... Python searching in the class scope after searching the instance scope, if it wasn't for the problem below... ...
    (comp.lang.python)
  • Re: Basic inheritance question
    ... Old Java habits die slowly. ... No, seriously it isn't Java habits only, most other languages wouldn't ... That's not very far from what a Python method object does - ... reference to the current instance is to pass it as an argument to the ...
    (comp.lang.python)
  • Re: variable declaration
    ... he might have been a Nutshell customer. ... to somebody who is not _happy_ to use Python. ... I did have good knowledge of a variety of languages, ... > paragraph early in the Nutshell book saying "there are no declarations, ...
    (comp.lang.python)
  • Re: Accessors in Python (getters and setters)
    ... Python does with is_active. ... vs Java's vs X's implementation of accessors and how using them sucks, ... And this is why many overzealous OO languages do "force" you to use ... def sayHello: ...
    (comp.lang.python)
  • Re: [OPINION] - does language really matter if they all do the same thing?
    ... > Are programming languages like spoken languages, ... > Can Python do something that PHP can't? ... > Can PHP do something that Ruby or Python can't? ...
    (comp.lang.python)