All this self dot is making me... 
Author Message
 All this self dot is making me...

To the beginner, having to type all these self. 's in class methods seems a
bit painful.  So all three of these methods below do the same thing.  That
leaves one to wonder "why have classes at all?"  A little thought and I
remember that this same thing is happening in C++: the compiler is adding a
this pointer to the passed parameters.  So Func and fooFunc are the same
thing.  The only difference between a method and a class method is
scope/access rules and tacking on this pointers, which in C++ is done by the
compiler. Correct?

So what has me perplexed is why all the self. 's?  Is this a namespace issue
or maybe just to expensive to do at runtime?

For those of us who have less cartilage left in their hands, would it be
acceptable to use "my." or better "me."  Or is self. strongly ingrained?
The two less characters add up and the keys are on stronger fingers.

nite,
Jim

def Func( this, a, b ):
  this.x = a + b

class Foo:
  def __init__( self ):
    self.x = 0
    self.f = Func
  def fooFunc( me, a, b ):
    me.x = a + b
  #if it was C++
  # fooFunc( a, b ) {
  # x = a + b; }

# who needs class methods?
z1 = Foo()
Func( z1, 5, 7 )
print z1.x
# it doesn't really matter if the method is in a class
z2 = Foo()
z2.f( z2, 5, 7 )
print z2.x
# this is the same as z1 approach
z3 = Foo()
z3.fooFunc( 5, 7 )
print z3.x



Sun, 18 Nov 2001 03:00:00 GMT  
 All this self dot is making me...

Quote:

> So what has me perplexed is why all the self. 's?  Is this a namespace issue
> or maybe just to expensive to do at runtime?

First, I find the immediate visual distinction between local variables
and object attributes helpful. Of course your opinion can differ!

Second, in the absence of variable declarations, python has no way
to find out what is local and what is an attribute. And this isn't made
easier by the fact that attributes can be created dynamically at any
time.

Quote:
> For those of us who have less cartilage left in their hands, would it be
> acceptable to use "my." or better "me."  Or is self. strongly ingrained?

Calling this parameter "self" is just convention, you can use whatever
you like. However, it's a convention that just about everybody follows,
so I'd think twice before doing something different.
--
-------------------------------------------------------------------------------

Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.55.69
Rue Charles Sadron                       | Fax:  +33-2.38.63.15.17
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais
-------------------------------------------------------------------------------


Sun, 18 Nov 2001 03:00:00 GMT  
 All this self dot is making me...

Quote:
>Calling this parameter "self" is just convention, you can use whatever
>you like. However, it's a convention that just about everybody follows,
>so I'd think twice before doing something different.

A couple of options come to mind:

Or call it something short - s. ? - and use a (careful) global search and
replace when  you're done with the coding.    I sometimes use this
for long identifiers.  

Or in VIM set up a mapping:
 :map! s. self.



Sun, 18 Nov 2001 03:00:00 GMT  
 All this self dot is making me...
:>Calling this parameter "self" is just convention, you can use whatever
:>you like. However, it's a convention that just about everybody follows,
:>so I'd think twice before doing something different.

: A couple of options come to mind:

: Or call it something short - s. ? - and use a (careful) global search and
: replace when  you're done with the coding.    I sometimes use this
: for long identifiers.  

: Or in VIM set up a mapping:
:  :map! s. self.

I think abbreviations would be better:
  :abbrev s. self.

The convention is to use "self".  It was the adoption of the original
object-oriented language (Smalltalk) in the early '70s.  Why did C++
change it to "this"?  I don't know.. probably just to be different.

People who are very well-versed in Python (Guido, Timbot, etc.)
probably wouldn't have a problem if someone used something other than
"self", but the average user might not.

Also from a psychological view, it might be better to start using
"self" instead of "this" to get your out of the "C++" thinking and into
a Python mode.. at least when you are programming in Python. :)

  -Arcege



Sun, 18 Nov 2001 03:00:00 GMT  
 All this self dot is making me...

Quote:

> The convention is to use "self".  It was the adoption of the original
> object-oriented language (SmallTalk) in the early '70s.  Why did C++
> change it to "this"?  I don't know.. probably just to be different.

SmallTalk was _not_ the first object-oriented language. Simula
introduced OO as early as 1967... Simula uses "this".

--

Stian Husemoen <stianh>



Sun, 18 Nov 2001 03:00:00 GMT  
 All this self dot is making me...

Quote:

> For those of us who have less cartilage left in their hands, would it be
> acceptable to use "my." or better "me."  Or is self. strongly ingrained?
> The two less characters add up and the keys are on stronger fingers.

First of all, you should probably look into an alternate keybard or keyboard
layout, such as Dvorak, if this is a chronic problem.  Also, drink lots of
water; I've found it to be the best defense against keyboard/mouse related
hand problems.  Also, use an abbreviation expansion mode of your editor,
or find a better editor; these are VERY helpful when using descriptive names.

Quote:
> def Func( this, a, b ):
>   this.x = a + b

> class Foo:
>   def __init__( self ):
>     self.x = 0
>     self.f = Func
>   def fooFunc( me, a, b ):
>     me.x = a + b

> # who needs class methods?
> z1 = Foo()
> Func( z1, 5, 7 )
> print z1.x

Numeric Python uses this approach (mainly), since these functions can then be
made to work on more than one type of object.  So, if Func() is meant to apply
to a number of objects, this is probably the way to go.  However, it would be
better written as:

def Func(a,b):
   return a + b

So that is doesn't rely on the behavior of "this", which would need to support
attribute assignment.  If you are going to alter an object's attributes, it is often
best to use a method on that object, to "encapsulate" its behavior.

But, if you really just want to save typing, I'd suggest letting the computer help
you, or change some habits, but don't make obfuscated code.  Using "this" instead
of "self" should work; but use it consistently.  I'd suggest the abbrev expansion
is a better solution for you.

Chad Netzer



Sun, 18 Nov 2001 03:00:00 GMT  
 All this self dot is making me...

Quote:
>For those of us who have less cartilage left in their hands, would it be
>acceptable to use "my." or better "me."

'Acceptable' to whom?  Python is a free language; use it as you wish 8).

For private work, I abbreviate 'self' to 's' and 'other' (in __add__(), etc)
to 'o'.  This works precisely because the existing conventions make it
obvious (to me) what is meant.  

TJ Reedy



Mon, 19 Nov 2001 03:00:00 GMT  
 All this self dot is making me...

Quote:

>To the beginner, having to type all these self. 's in class methods seems a
>bit painful.  So all three of these methods below do the same thing.  That
>For those of us who have less cartilage left in their hands, would it be
>acceptable to use "my." or better "me."  Or is self. strongly ingrained?
>The two less characters add up and the keys are on stronger fingers.

It's absolutely acceptable to use something else.  It's good to make it
clear, so "self" is the best, "this" works well, "me" and "my" are fine,
and "snowman" is abominable.

The reason you don't want to use external functions is that object
orientation doesn't work on them -- you can't override them.

Quote:
>Jim

--
-William "Billy" Tanksley
Utinam logica falsa tuam philosophiam totam suffodiant!
   :-: May faulty logic undermine your entire philosophy!


Tue, 20 Nov 2001 03:00:00 GMT  
 All this self dot is making me...

Quote:
>I think abbreviations would be better:
>  :abbrev s. self.

Give it a shot.   The abbreviation has to been seen as a word before it is
replaced - that is you must have white space after hitting 's.'  So then you
you have to back up to just after the .

The mapping runs as soon as you hit the '.' and you can continue typing on the
fly.



Tue, 20 Nov 2001 03:00:00 GMT  
 All this self dot is making me...

Quote:


> >I think abbreviations would be better:
> >  :abbrev s. self.

> Give it a shot.   The abbreviation has to been seen as a word before it is
> replaced - that is you must have white space after hitting 's.'  So then you
> you have to back up to just after the .

> The mapping runs as soon as you hit the '.' and you can continue typing on the
> fly.

And a similiar problem with mapping .s to self:

"There she was"
becomes:"there she waself"

--
Bill Anderson                                   Linux Administrator

My opinions are just that; _my_ opinions.



Tue, 04 Dec 2001 03:00:00 GMT  
 All this self dot is making me...

I'd suggest the potent combination of (X)Emacs "dabbrev" mode and
Python-mode.

Dabbrev has probably saved me from carpal tunnel syndrome...



Tue, 04 Dec 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. dot-dot-dot

2. Are Dot Matrix Printer Sharing Boxes still made??

3. Making the attribute 'self' preferred

4. Self Made Locator

5. Colors in "self-made" queue-browse

6. maybe a self made problem with win32all.exe

7. I am making aplications in clarion for windows.

8. Comma dot (1,000.00) to dot comma (1.000,00)

9. Self-reproduction or self-changing of Web pages

10. Self-reproduction or self-change of Web page

11. Self-reproduction or self-changing of Web pages

12. self-replicating-code, self-replicating-messages

 

 
Powered by phpBB® Forum Software