confused about self, why not a reserved word?

Discussion in 'Python' started by globalrev, May 5, 2008.

  1. globalrev

    globalrev Guest

    class Foo(object):
    def Hello(self):
    print "hi"

    object is purple, ie some sort of reserved word.

    why is self in black(ie a normal word) when it has special powers.
    replacing it with sel for example will cause an error when calling
    Hello.
    globalrev, May 5, 2008
    #1
    1. Advertising

  2. 2008/5/5, globalrev <>:
    > class Foo(object):
    > def Hello(self):
    > print "hi"
    >
    > object is purple, ie some sort of reserved word.
    >
    > why is self in black(ie a normal word) when it has special powers.
    > replacing it with sel for example will cause an error when calling
    > Hello.


    Could you give an example of such an error?

    --
    Regards,
    Wojtek Walczak
    http://www.stud.umk.pl/~wojtekwa/
    Wojciech Walczak, May 5, 2008
    #2
    1. Advertising

  3. globalrev <> wrote:
    > class Foo(object):
    > def Hello(self):
    > print "hi"
    >
    > object is purple, ie some sort of reserved word.
    >
    > why is self in black(ie a normal word) when it has special powers.


    Because `self` is just a name. Using `self` is a convention, but one can
    use any name which is not reserved.

    > replacing it with sel for example will cause an error when calling
    > Hello.


    Which error? I don't get one.

    0:tolot:/tmp> python f.py
    hi
    0:tolot:/tmp> cat f.py
    class Foo(object):
    def Hello(sel):
    print "hi"

    Foo().Hello()
    0:tolot:/tmp>

    Marc
    Marc Christiansen, May 5, 2008
    #3
  4. globalrev wrote:

    > class Foo(object):
    > def Hello(self):
    > print "hi"
    >
    > object is purple, ie some sort of reserved word.


    It is not.

    > why is self in black(ie a normal word) when it has special powers.
    > replacing it with sel for example will cause an error when calling
    > Hello.


    That must be some other problem. This is perfectly legal python code and
    works:

    class Foo(object):
    def bar(imafancyobject):
    print imafancyobject

    f = Foo()
    f.bar()

    Diez
    Diez B. Roggisch, May 5, 2008
    #4
  5. On Mon, 2008-05-05 at 04:57 -0700, globalrev wrote:
    > class Foo(object):
    > def Hello(self):
    > print "hi"
    >
    > object is purple, ie some sort of reserved word.
    >
    > why is self in black(ie a normal word) when it has special powers.
    > replacing it with sel for example will cause an error when calling
    > Hello.
    >


    Wow. No it won't. sel is perfectly legal. I've seen s used on
    occasion, and of course metaclass methods often use cls.

    Direct ^C^V from my python interpreter:

    $ python
    Python 2.4.3 (#1, Dec 11 2006, 11:38:52)
    [GCC 4.1.1 20061130 (Red Hat 4.1.1-43)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class Foo(object):

    .... def Hello(sel):
    .... print "hi"
    ....
    >>> f = Foo()
    >>> f.Hello()

    hi
    >>>


    Can you paste an example that breaks for you?


    --
    Oook,
    J. Cliff Dyer
    Carolina Digital Library and Archives
    UNC Chapel Hill
    J. Cliff Dyer, May 5, 2008
    #5
  6. globalrev

    globalrev Guest

    sorry i noticed now a little error, i was playing around a lot and saw
    that on the sel-example i had then called foo.hello() instead of
    foo.Hello().


    but it was an informing error now i egt it. was wondering about self
    anyway so thanks for clearing it up.


    but it still is a bit confusing that u can pass with any word.
    wouldnt:


    class Foo(object):
    def Hello():
    print "hi"


    make more sense?

    the class-thing seems inside out somehow in python. if im doing
    foo.Hello() im already saying that im using the class foo because i
    did foo=Foo() before and then still when calling Hello() i am
    invisibly passing the class itself a s a parameter by default?
    just seems backwards and weird.
    globalrev, May 5, 2008
    #6
  7. globalrev

    Guest

    > the class-thing seems inside out somehow in python. if im doing
    > foo.Hello() im already saying that im using the class foo because i
    > did foo=Foo() before and then still when calling Hello() i am
    > invisibly passing the class itself a s a parameter by default?
    > just seems backwards and weird.


    You're not passing a reference to the class, you're passing a
    reference to the object. Any object-orientated programming language
    will be passing methods references to an object; Python just makes
    that explicit within the method definition. See http://www.python.org/doc/faq/general/#id35
    , May 5, 2008
    #7
  8. globalrev a écrit :
    > sorry i noticed now a little error, i was playing around a lot and saw
    > that on the sel-example i had then called foo.hello() instead of
    > foo.Hello().
    >
    >
    > but it was an informing error now i egt it. was wondering about self
    > anyway so thanks for clearing it up.
    >
    >
    > but it still is a bit confusing that u can pass with any word.
    > wouldnt:
    >
    >
    > class Foo(object):
    > def Hello():
    > print "hi"
    >
    >
    > make more sense?


    What would make sens in your example would be to either make hello a
    staticmethod, since it doesn't use any reference to the current instance
    nor it's class.


    > the class-thing seems inside out somehow in python.


    Python's object model is indeed a bit pecular wrt/ most mainstream
    OOPLs. But once you understand how it works, it happens to be mostly
    well designed (IMHO) and really powerful.

    > if im doing
    > foo.Hello() im already saying that im using the class foo because i
    > did foo=Foo()


    Nope. You are saying your using an instance of class Foo.

    > before and then still when calling Hello() i am
    > invisibly passing the class itself a s a parameter by default?


    It's not the class that get passed (unless you use a classmethod, but
    that's not the case here), but the instance. And you are not "invisibly"
    passing it, you just pass it another way. In fact, foo.hello() is
    equivalent to type(foo).hello(foo).

    > just seems backwards and weird.


    Each and every OOPL needs to have a way to "inject" the instance a
    method was called on into the method's local namespace. Python solved
    this it's own way - by making 'methods' thin wrappers around
    instance/function couples, these wrappers being in charge of calling the
    function with the instance as first argument. From the implementation
    POV, this allow to build methods on existing, more general features -
    functions and the descriptor protocol - instead of having to
    special-case them. From the programmer's POV, this allow to define
    functions outside classes, then dynamically add them as 'methods' either
    on a per-class or per-instance basis.
    Bruno Desthuilliers, May 5, 2008
    #8
  9. globalrev

    Terry Reedy Guest

    "globalrev" <> wrote in message
    news:...
    | sorry i noticed now a little error, i was playing around a lot and saw
    | that on the sel-example i had then called foo.hello() instead of
    | foo.Hello().

    Lesson: whenever one inquires about an 'error', one should cut and paste
    the error traceback along with a relevant snippet of code.
    Terry Reedy, May 5, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. metaperl
    Replies:
    32
    Views:
    892
    NickC
    Sep 15, 2006
  2. Delaney, Timothy (Tim)
    Replies:
    10
    Views:
    659
    Jason
    Sep 14, 2006
  3. Steve Holden
    Replies:
    0
    Views:
    388
    Steve Holden
    Sep 13, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,761
    Smokey Grindel
    Dec 2, 2006
  5. Replies:
    3
    Views:
    152
    osmium
    Nov 6, 2013
Loading...

Share This Page