Click to See Complete Forum and Search --> : Python Prob: "self"
CaptainPinko
Mar 12th, 2002, 02:09 PM
what does "self" mean in Phython? is it like "this" in Java/JavaScript? why is it when it insert it into a function definition i don't need insert a value for it? does it automatically pass itself for "self"? and can you make any "static" functions in Phython like in Java? does every class method need to have "self" in it? can a class function be called w/o an instantiation?
ANY answer is good! thnx
progressive
Mar 13th, 2002, 04:18 AM
taken from the python doc's
Conventionally, the first argument of methods is often
called self. This is nothing more than a convention: the name self
has absolutely no special meaning to Python. (Note, however,
that by not following the convention your code may be less readable by other Python programmers, and it is also conceivable
that a class browser program be written which relies upon such a
convention.)
*shrugs shoulders*
JoshT
Mar 13th, 2002, 06:36 AM
Generally, in any OO programming, the a Class's method's first argument is a hidden one that gets a pointer to the specific object to work on. Its the reason with VB you can't use AddressOf to pass an API call a function in a Class modules.
CaptainPinko
Mar 13th, 2002, 12:06 PM
progressive:
i read the docs and i saw that but i don't really know what that means. why does this work:
>>> class ASimpleClass:
def a (self):
print "hello world"
>>> a = ASimpleClass()
>>> a.a()
hello world
but this doesn't
>>> class ASimpleClass:
def a ():
print "hello world"
>>> a = ASimpleClass()
>>> a.a()
Traceback (most recent call last):
File "<pyshell#10>", line 1, in ?
a.a()
TypeError: a() takes no arguments (1 given)
so despite the docs, "self" apparently does have a special meanin' as i need to declare it as an argument even even i'm not passing anything
try the code yourself, i'm using IDLE (the editor) and the colours should match too! :P
PS: who came up w/ the idea of spelling colour as "color"?
progressive
Mar 14th, 2002, 03:16 AM
*shrugs shoulders again*
I'll go with JoshT on this one, I wasn't aware of this hidden pointer practice with 00 languages.
JoshT: Can you ellaborate on why Python chooses not to hide the pointer argument ?
JoshT
Mar 14th, 2002, 10:54 AM
Well, learning Python is still on my "TO-DO" list, but I'd guess either because its an interpreted language or its just the author's personal preference. If you look at OO Perl, its pretty much the same.
CaptainPinko
Mar 15th, 2002, 10:57 AM
hhmmm, makes sense, but it's rather silly to have to declare it and you don't really need to pass it and the compiler\interpreter KNOWS you need to have it there!!
CiberTHuG
Mar 19th, 2002, 12:07 PM
I'm with Josh, I haven't learned PHP, yet. But unlike Josh, I don't know the OO side of Perl that well.
I do know that in C (or C++) you can have a function with the same name, but different number or type of arguments. Perhaps you can do this...
>>> class ASimpleClass:
def a (self):
print "hello world"
def a ():
print "hello world"
In which the first a is a method of the class but the second a is a private internal function.
I don't really have a clue, I'm just guessing.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.