Results 1 to 8 of 8

Thread: Python Prob: "self"

  1. #1

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Question Python Prob: "self"

    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
    Last edited by CaptainPinko; Mar 12th, 2002 at 03:28 PM.
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  2. #2
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    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*

  3. #3
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    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.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  4. #4

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Angry frustrating...

    progressive:

    i read the docs and i saw that but i don't really know what that means. why does this work:
    Code:
    >>> class ASimpleClass:
    	def a (self):
    		print "hello world"
    
    		
    >>> a = ASimpleClass()
    >>> a.a()
    hello world
    but this doesn't
    Code:
    >>> 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"?
    Last edited by CaptainPinko; Mar 13th, 2002 at 01:19 PM.
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  5. #5
    Hyperactive Member progressive's Avatar
    Join Date
    Sep 2001
    Location
    Manchester, UK
    Posts
    404
    *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 ?

  6. #6
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    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.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  7. #7

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Thumbs down

    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!!
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  8. #8
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    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...

    PHP Code:
    >>> 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.
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width