FAQ
I am very new to Python, but it appears that every function defined in a
class must have self as a first variable name. Assuming that is the
case, it would be nice if there was an error message given when the
interpreter encounters a function that is defined without self (rather
than giving the error message "TypeError: no arguments expected" when
the function is called. Even better, it would be nice (probably too late
I know), if self was defined implicitly as the "this" pointer is in C++.


Sent via Deja.com http://www.deja.com/
Before you buy.

Search Discussions

  • Moshe Zadka at Feb 26, 2000 at 10:45 am

    On Sat, 26 Feb 2000 london999 at my-deja.com wrote:

    I am very new to Python, but it appears that every function defined in a
    class must have self as a first variable name.
    No, it can call the argument what it likes. "self" is the most popular,
    by far. "me", "my" and "s" are poor contestants most people don't even
    know about. But it's a convention, not a rule.
    Assuming that is the
    case, it would be nice if there was an error message given when the
    interpreter encounters a function that is defined without self (rather
    than giving the error message "TypeError: no arguments expected" when
    the function is called.
    It can't. Consider:

    class foo:
    pass

    def bar():
    pass

    foo.bar = bar

    a = foo()
    a.bar()

    The only time the interpreter can give the error is when a.bar() is
    called, and that's when it does.
    Even better, it would be nice (probably too late
    I know), if self was defined implicitly as the "this" pointer is in C++.
    That would add another keyword to the language, and contradict one of
    Python's principles "explicit is better then implicit"
    --
    Moshe Zadka <mzadka at geocities.com>.
    INTERNET: Learn what you know.
    Share what you don't.
  • Jason Stokes at Feb 26, 2000 at 6:17 pm
    london999 at my-deja.com wrote in message <8984mm$5pi$1 at nnrp1.deja.com>...
    I am very new to Python, but it appears that every function defined in a
    class must have self as a first variable name.
    No. You're just a little confused on a couple of points. Firstly, member
    functions always receive the invoked upon class instance as the first
    argument. The convention is to call this argument "self", but there's no
    requirement to do so. Secondly, not all functions are members of a class.
    Functions that are not class members don't receive this implicit argument;
    this way, you can program in conventional functional or procedural style.
    Even better, it would be nice (probably too late
    I know), if self was defined implicitly as the "this" pointer is in C++.

    I'm not sure why Python's way was chosen, but you get used to it.
  • James Logajan at Feb 26, 2000 at 7:16 pm

    Jason Stokes wrote:
    london999 at my-deja.com wrote in message <8984mm$5pi$1 at nnrp1.deja.com>...
    I am very new to Python, but it appears that every function defined in a
    class must have self as a first variable name.
    No. You're just a little confused on a couple of points. Firstly, member
    functions always receive the invoked upon class instance as the first
    argument. The convention is to call this argument "self", but there's no
    requirement to do so. Secondly, not all functions are members of a class.
    Functions that are not class members don't receive this implicit argument;
    this way, you can program in conventional functional or procedural style.
    Even better, it would be nice (probably too late
    I know), if self was defined implicitly as the "this" pointer is in C++.
    I'm not sure why Python's way was chosen, but you get used to it.
    If self were defined implicitly, then you would have to explicitly define
    local variables. This is, today you would do something like:

    class C:
    def __init__(self, b):
    self.b = b
    self.x = self.y = 0
    def Func(self, a):
    t = a * self.b
    self.x = t * 2
    self.y = t + 2

    If self were implicit, then variables like 't' would need to be declared.
    That leads to some problems. The above might become:

    class C:
    def __init__(self, b):
    b = local.b # Arguments are local.
    def Func(self, a):
    local.t = local.a * b
    x = local.t * 2
    y = local.t + 2

    Or they could be declared once anywhere in the scope:

    class C:
    def __init__(self, b):
    b = local.b # Still need to
    def Func(self, a):
    local t = a * b
    x = t * 2
    y = t + 2


    I think I like the way the language was designed.
  • Gregoire Welraeds at Feb 28, 2000 at 12:04 pm
    In reply to the message of James Logajan sent on Feb 26 (see below) :

    In my newbiness ignorance, I'm asking what is the difference between t and
    self.x. Aren't they both member of the class C ?
    class C:
    def __init__(self, b):
    self.b = b
    self.x = self.y = 0
    def Func(self, a):
    t = a * self.b
    self.x = t * 2
    self.y = t + 2



    --
    Life is not fair
    But the root password helps
    --

    Gregoire Welraeds
    greg at perceval.be
    Perceval Development team
    -------------------------------------------------------------------------------
    Perceval Technologies sa/nv Tel: +32-2-6409194
    Rue Tenbosch, 9 Fax: +32-2-6403154
    B-1000 Brussels general information: info at perceval.net
    BELGIUM technical information: helpdesk at perceval.net
    URL: http://www.perceval.be/
    -------------------------------------------------------------------------------
    On Sat, 26 Feb 2000, James Logajan wrote:

    Date: Sat, 26 Feb 2000 11:16:56 -0800
    From: James Logajan <JamesL at Lugoj.Com>
    To: python-list at python.org
    Newsgroups: comp.lang.python
    Subject: Re: self argument

    Jason Stokes wrote:
    london999 at my-deja.com wrote in message <8984mm$5pi$1 at nnrp1.deja.com>...
    I am very new to Python, but it appears that every function defined in a
    class must have self as a first variable name.
    No. You're just a little confused on a couple of points. Firstly, member
    functions always receive the invoked upon class instance as the first
    argument. The convention is to call this argument "self", but there's no
    requirement to do so. Secondly, not all functions are members of a class.
    Functions that are not class members don't receive this implicit argument;
    this way, you can program in conventional functional or procedural style.
    Even better, it would be nice (probably too late
    I know), if self was defined implicitly as the "this" pointer is in C++.
    I'm not sure why Python's way was chosen, but you get used to it.
    If self were defined implicitly, then you would have to explicitly define
    local variables. This is, today you would do something like:

    class C:
    def __init__(self, b):
    self.b = b
    self.x = self.y = 0
    def Func(self, a):
    t = a * self.b
    self.x = t * 2
    self.y = t + 2

    If self were implicit, then variables like 't' would need to be declared.
    That leads to some problems. The above might become:

    class C:
    def __init__(self, b):
    b = local.b # Arguments are local.
    def Func(self, a):
    local.t = local.a * b
    x = local.t * 2
    y = local.t + 2

    Or they could be declared once anywhere in the scope:

    class C:
    def __init__(self, b):
    b = local.b # Still need to
    def Func(self, a):
    local t = a * b
    x = t * 2
    y = t + 2


    I think I like the way the language was designed.
    --
    http://www.python.org/mailman/listinfo/python-list
  • Gerrit Holl at Feb 28, 2000 at 12:20 pm

    On Mon, Feb 28, 2000 at 01:04:59PM +0100, Gregoire Welraeds wrote:
    In reply to the message of James Logajan sent on Feb 26 (see below) :

    In my newbiness ignorance, I'm asking what is the difference between t and
    self.x. Aren't they both member of the class C ?
    No, only 'self.x' is. 't' is only in the local namespace; it gets
    destroyed as soon as the function (method, in this case) is ready.

    regards,
    Gerrit.

    --
    -----BEGIN GEEK CODE BLOCK----- http://www.geekcode.com
    Version: 3.12
    GCS dpu s-:-- a14 C++++>$ UL++ P--- L+++ E--- W++ N o? K? w--- !O !M !V PS+ PE?
    Y? PGP-- t- 5? X? R- tv- b+(++) DI D+ G++ !e !r !y
    -----END GEEK CODE BLOCK-----
  • Mikael Olofsson at Feb 28, 2000 at 12:25 pm

    On 28-Feb-00 Gregoire Welraeds wrote:
    In reply to the message of James Logajan sent on Feb 26 (see below) : >
    In my newbiness ignorance, I'm asking what is the difference between t and
    self.x. Aren't they both member of the class C ?
    >
    class C:
    def __init__(self, b):
    self.b = b
    self.x = self.y = 0
    def Func(self, a):
    t = a * self.b
    self.x = t * 2
    self.y = t + 2
    No, t is a local variable in C.Func, while self.x refers to a member.
    Thus, if you have an instance c of class C, there will never be a c.t,
    but there is a c.b, a c.x, and a c.y.

    /Mikael

    -----------------------------------------------------------------------
    E-Mail: Mikael Olofsson <mikael at isy.liu.se>
    WWW: http://www.dtr.isy.liu.se/dtr/staff/mikael
    Phone: +46 - (0)13 - 28 1343
    Telefax: +46 - (0)13 - 28 1339
    Date: 28-Feb-00
    Time: 13:19:32

    This message was sent by XF-Mail.
    -----------------------------------------------------------------------
  • Gregoire Welraeds at Feb 28, 2000 at 1:50 pm
    I can't access the __init__ method outside of the object, so the
    following is disallowed :
    ----
    class huh():
    def __init(self):
    [some initialisation]

    ough= huh()

    [some code]

    ough.__init__()
    ----

    Right ?
    The following should be allowed ?

    class huh():
    def __init__(self):
    [some initialisation]

    def ReInit(self):
    self.__init__()

    ough= huh()
    [some code]
    ough.ReInit()

    Do I have to rewrite the __init__ function in the ReInit() or the call to
    the init() function inside the ReInit is enough ?
    Isn't there a better way to do this kind of job, i mean in a OOP point of
    view.

    Other little question, what about the following (regarding another remark
    posted before) :

    def __init__(this):
    ...

    --
    Life is not fair
    But the root password helps
    --

    Gregoire Welraeds
    greg at perceval.be
    Perceval Development team
    -------------------------------------------------------------------------------
    Perceval Technologies sa/nv Tel: +32-2-6409194
    Rue Tenbosch, 9 Fax: +32-2-6403154
    B-1000 Brussels general information: info at perceval.net
    BELGIUM technical information: helpdesk at perceval.net
    URL: http://www.perceval.be/
    -------------------------------------------------------------------------------
  • Justin Sheehy at Feb 28, 2000 at 3:03 pm

    Gregoire Welraeds <greg at perceval.be> writes:

    I can't access the __init__ method outside of the object, so the
    following is disallowed :
    ----
    class huh():
    def __init(self):
    [some initialisation]

    ough= huh()

    [some code]

    ough.__init__()
    ----
    If you fix the two minor errors in your code, the above works fine:
    class huh:
    ... def __init__(self):
    ... print 'spam'
    ...
    ough = huh()
    spam
    ough.__init__()
    spam

    Other little question, what about the following (regarding another remark
    posted before) :

    def __init__(this):
    Sure, that's fine. There is nothing special about 'self', it is
    merely a convention. The instance is passed as the first argument to
    methods; the position matters, not the name.

    -Justin
  • Grant Edwards at Feb 26, 2000 at 8:15 pm

    In article <pOUt4.34424$3b6.163759 at ozemail.com.au>, Jason Stokes wrote:

    Even better, it would be nice (probably too late I know), if self
    was defined implicitly as the "this" pointer is in C++.
    I'm not sure why Python's way was chosen, but you get used to it.
    Gee, I always though C++ was the "odd" language and that it sure would be nice
    if the "this" pointer was passed as a parameter to methods and had to be used to
    access instance variables.

    --
    Grant Edwards grante Yow! I want to dress you
    at up as TALLULAH BANKHEAD and
    visi.com cover you with VASELINE and
    WHEAT THINS...
  • Ken Seehof at Feb 28, 2000 at 5:07 pm

    Gregoire Welraeds wrote:

    I can't access the __init__ method outside of the object, so the
    following is disallowed :
    ----
    class huh():
    def __init(self):
    [some initialisation]

    ough= huh()

    [some code]

    ough.__init__()
    ----

    Right ?
    The following should be allowed ?

    class huh():
    def __init__(self):
    [some initialisation]

    def ReInit(self):
    self.__init__()

    ough= huh()
    [some code]
    ough.ReInit()

    Do I have to rewrite the __init__ function in the ReInit() or the call to
    the init() function inside the ReInit is enough ?
    Isn't there a better way to do this kind of job, i mean in a OOP point of
    view.
    Since the previous reply answers your first question, I'll talk aboutprogramming
    style. I would tend to write a ReInit function since
    __init__ really means "called when you create the object". However,
    there is nothing illegal about calling __init__ directly.

    class huh:
    def __init__(self):
    # some one-time initialization code
    print "spammity"
    self.ReInit()

    def ReInit(self):
    # some code
    print "spam"
    h = huh()
    spammity
    spam
    h.ReInit()
    spam

    The only time __init__ is usually called is from a derived object:

    class what(huh):
    def __init__(self):
    huh.__init__(self)
    # more initialization for what
    Other little question, what about the following (regarding another remark
    posted before) :

    def __init__(this):
    Legal, but don't do that. People will think you are a C programmer :-) Ack!
    --
    Life is not fair
    But the root password helps
    --

    Gregoire Welraeds
    greg at perceval.be
    Perceval Development team
    -------------------------------------------------------------------------------
    Perceval Technologies sa/nv Tel: +32-2-6409194
    Rue Tenbosch, 9 Fax: +32-2-6403154
    B-1000 Brussels general information: info at perceval.net
    BELGIUM technical information: helpdesk at perceval.net
    URL: http://www.perceval.be/
    -------------------------------------------------------------------------------
  • Gregoire Welraeds at Feb 28, 2000 at 4:36 pm
    In reply to the message of Ken Seehof sent on Feb 28 (see below) :

    thanks for all reply.

    until-next-"this-looks-like-another-newbie"-question'ly yours :)

    --
    Life is not fair
    But the root password helps
    --

    Gregoire Welraeds
    greg at perceval.be
    Perceval Development team
    -------------------------------------------------------------------------------
    Perceval Technologies sa/nv Tel: +32-2-6409194
    Rue Tenbosch, 9 Fax: +32-2-6403154
    B-1000 Brussels general information: info at perceval.net
    BELGIUM technical information: helpdesk at perceval.net
    URL: http://www.perceval.be/
    -------------------------------------------------------------------------------
    On Mon, 28 Feb 2000, Ken Seehof wrote:

    Date: Mon, 28 Feb 2000 09:07:47 -0800
    From: Ken Seehof <kens at sightreader.com>
    To: python-list at python.org
    Newsgroups: comp.lang.python
    Subject: Re: Killin' Newbie question

    Gregoire Welraeds wrote:
    I can't access the __init__ method outside of the object, so the
    following is disallowed :
    ----
    class huh():
    def __init(self):
    [some initialisation]

    ough= huh()

    [some code]

    ough.__init__()
    ----

    Right ?
    The following should be allowed ?

    class huh():
    def __init__(self):
    [some initialisation]

    def ReInit(self):
    self.__init__()

    ough= huh()
    [some code]
    ough.ReInit()

    Do I have to rewrite the __init__ function in the ReInit() or the call to
    the init() function inside the ReInit is enough ?
    Isn't there a better way to do this kind of job, i mean in a OOP point of
    view.
    Since the previous reply answers your first question, I'll talk aboutprogramming
    style. I would tend to write a ReInit function since
    __init__ really means "called when you create the object". However,
    there is nothing illegal about calling __init__ directly.

    class huh:
    def __init__(self):
    # some one-time initialization code
    print "spammity"
    self.ReInit()

    def ReInit(self):
    # some code
    print "spam"
    h = huh()
    spammity
    spam
    h.ReInit()
    spam

    The only time __init__ is usually called is from a derived object:

    class what(huh):
    def __init__(self):
    huh.__init__(self)
    # more initialization for what
    Other little question, what about the following (regarding another remark
    posted before) :

    def __init__(this):
    Legal, but don't do that. People will think you are a C programmer :-) Ack!
    --
    Life is not fair
    But the root password helps
    --

    Gregoire Welraeds
    greg at perceval.be
    Perceval Development team
    -------------------------------------------------------------------------------
    Perceval Technologies sa/nv Tel: +32-2-6409194
    Rue Tenbosch, 9 Fax: +32-2-6403154
    B-1000 Brussels general information: info at perceval.net
    BELGIUM technical information: helpdesk at perceval.net
    URL: http://www.perceval.be/
    -------------------------------------------------------------------------------


    --
    http://www.python.org/mailman/listinfo/python-list

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 26, '00 at 9:00a
activeFeb 28, '00 at 5:07p
posts12
users10
websitepython.org

People

Translate

site design / logo © 2022 Grokbase