Inheriting the @ sign from Ruby

Bjorn Pettersen pbjorn at uswest.net
Tue Dec 12 12:35:07 EST 2000


Roy Katz wrote:

> Hello,
>
> I was reading a Dr. Dobb's recently and it mentioned that Ruby uses the @
> symbol to denote instance members of a class.  This appeals to me because
> I can comprehend it faster than seeing 'self' pasted in front of a
> name, especially when the statement is buried in dense code.
>
> Why not bind @ to the first argument of method definitions? Consider:
>
>   class X:
>     def __init__( self, x ):
>       @bind(x)     # binds to 'self', because
>                    # 'self' is the first argument to __init__
>     def bind(myself, x):
>       @x = x   # equivalent to myself.x
>
> What faults or strengths do you see with this?

Besides being visually unpleasant, it wouldn't work for the following code:

   class Outer:
      class Inner:
         def __init__(self, x):
            self.x = x
      def __init__(self, x):
         self.x = x

and various other variations (like defining a class inside a method etc.)
While these cases aren't particularly common in current Python code, they
might be when/if PEP-227 (http://python.sourceforge.net/peps/pep-0227.html)
makes it into the next version (discussion on whether functions are first
class objects in Python can now comense <wink>).

-- bjorn





More information about the Python-list mailing list