Re: Confused with methods
From: Matthias Kempka (lists_at_mkempka.de)
Date: 02/06/05
- Next message: Alex Martelli: "Re: Python versus Perl ?"
- Previous message: Johannes Ahl-mann: "Re: bad generator performance"
- Maybe in reply to: jfj: "Confused with methods"
- Next in thread: Dan Perl: "Re: Confused with methods"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 06 Feb 2005 17:19:52 +0100 To: python-list@python.org
Hi Gerald,
When you define an instance method, the first parameter (in the
definition) represents the instance. By convention, you would name it
"self":
#####################
class B:
def foo(self, x):
print "we have two parameters: " + str(self) + " and " + x
#####################
then calling
######################
b = B()
b.foo("x")
######################
would be equivalent to
######################
b = B()
B.foo(b, "x")
######################
So, as you have noted, you need at least one parameter to attach the
method to an instance. This is because the instance _is_ the parameter.
Python does this for you internally.
For more documentation you should read the paragraph about classes in
the tutorial.
Regards,
Matthias
- Next message: Alex Martelli: "Re: Python versus Perl ?"
- Previous message: Johannes Ahl-mann: "Re: bad generator performance"
- Maybe in reply to: jfj: "Confused with methods"
- Next in thread: Dan Perl: "Re: Confused with methods"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|