FAQ
Hi, i`m a newbie in python, and i was reading the tutorial (and some
source codes), then i noticed that every function inside a class
receives the 'self' parameter.

Like this exemple in python tutorial:

---
class MyClass:
"A simple example class"
i = 12345
def f(self):
return 'hello world'
---

I think self points to the class, but why do we need to pass this
parameter to every function?

Search Discussions

  • Sismex01 at Feb 11, 2003 at 7:51 pm

    From: nvivo at terra.com.br [mailto:nvivo at terra.com.br]
    Sent: Tuesday, February 11, 2003 1:48 PM

    Hi, i`m a newbie in python, and i was reading the tutorial (and some
    source codes), then i noticed that every function inside a class
    receives the 'self' parameter.

    Like this exemple in python tutorial:

    ---
    class MyClass:
    "A simple example class"
    i = 12345
    def f(self):
    return 'hello world'
    ---

    I think self points to the class, but why do we need to pass this
    parameter to every function?
    "self" is the current instance, and as to why make it
    explicit instead of implicit (like C++), that decision
    was made by Guido van Rossum a long time ago.

    -gustavo
  • Grant Edwards at Feb 11, 2003 at 8:23 pm

    In article <bbb4a9e1.0302111148.48dc074b at posting.google.com>, Natan wrote:

    I think self points to the class,
    No, self points to an instance of the class.
    but why do we need to pass this parameter to every function?
    While you have to declare it, you don't have to pass it when
    you're calling a bound method. You only have to pass it when
    calling an unbound method:

    class A:
    def foo(self,arg):
    print "foo:",`self`,arg
    def bar(self,arg):
    print "bar:",`self`,arg

    a1 = A()
    a2 = A()

    a1.foo("asdf")
    A.foo(a1,"asdf")

    a2.foo("qwer")
    A.foo(a2,"qwer")

    a1.bar(3.14159)
    A.bar(a1,42)



    --
    Grant Edwards grante Yow! .. Do you like
    at "TENDER VITTLES?"?
    visi.com
  • Rene Pijlman at Feb 11, 2003 at 9:02 pm
    Natan:
    Hi, i`m a newbie in python, and i was reading the tutorial (and some
    source codes), then i noticed that every function inside a class
    receives the 'self' parameter. [...]
    I think self points to the class, but why do we need to pass this
    parameter to every function?
    This is a FAQ. You know the FAQ, don't you? I'm also a newbie,
    but I've read it and that makes the difference :-)

    "6.7. Why must 'self' be declared and used explicitly in method
    definitions and calls?

    When classes were added to Python, this was (again) the simplest
    way of implementing methods without too many changes to the
    interpreter. The idea was borrowed from Modula-3. It turns out
    to be very useful, for a variety of reasons.
    [...]
    First, it makes it more obvious that you are using a method or
    instance attribute instead of a local variable.
    [...]
    Second, it means that no special syntax is necessary if you want
    to explicitly reference or call the method from a particular
    class."
    http://www.python.org/cgi-bin/faqw.py?req=show&file=faq06.007.htp

    --
    Ren? Pijlman

    Wat wil jij leren? http://www.leren.nl
  • Marshall at Feb 12, 2003 at 2:47 am
    nvivo at terra.com.br (Natan) wrote in message news:<bbb4a9e1.0302111148.48dc074b at posting.google.com>...
    Hi, i`m a newbie in python, and i was reading the tutorial (and some
    source codes), then i noticed that every function inside a class
    receives the 'self' parameter.

    Like this exemple in python tutorial:

    ---
    class MyClass:
    "A simple example class"
    i = 12345
    def f(self):
    return 'hello world'
    ---

    I think self points to the class, but why do we need to pass this
    parameter to every function?
    'self' refers to the instance of the class (object) whose method is
    being invoked. It has to get passed to the object when calling the
    method so that the method knows what object it is working on (in?).
    I'm not sure that is 100% correct but that is how I visualize it. (I
    also visualize all my objects wearing nametags that say 'self', upside
    down so they can read them of course.)

    One thing that you may have already noticed is that 'self' is
    automatically passed when the method is called.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 11, '03 at 7:48p
activeFeb 12, '03 at 2:47a
posts5
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase