Python
  Home arrow Python arrow Page 5 - Object Oriented Programming With Python (part 1)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PYTHON

Object Oriented Programming With Python (part 1)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 28
    2001-11-01


    Table of Contents:
  • Object Oriented Programming With Python (part 1)
  • A Very Big Snake
  • What's In A Name?
  • Digging Deep
  • Self-Involved
  • Under Construction

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Object Oriented Programming With Python (part 1) - Self-Involved
    ( Page 5 of 6 )

    You'll have noticed, in the examples above, a curious little thingummy called "self". And you're probably wondering what exactly it has to do with anything.

    The "self" variable has a lot to do with the way class methods work in Python. When a class method is called, it requires to be passed a reference to the instance that is calling it. This reference is passed as the first argument to the method, and is represented by the "self" variable.

    class veryBigSnake: # some useful methods def set_snake_name(self, name): self.name = name
    An example of this can be seen in the set_snake_name() function above. When an instance of the class calls this method, a reference to the instance is automatically passed to the method, together with the additional "name" parameter. This reference is then used to update the "name" variable belonging to that specific instance of the class.

    Here's an example of how this works:

    >>> alpha = veryBigSnake() >>> alpha.set_snake_name("Peter Python") >>> alpha.name 'Peter Python' >>>
    Note that when you call a class method from an instance, you do not need to explicitly pass this reference to the method - Python takes care of this for you automatically.

    With that in mind, consider this:

    >>> alpha = test.veryBigSnake() >>> veryBigSnake.set_snake_name(alpha, "Peter Python") >>> alpha.name 'Peter Python' >>>
    In this case, I'm calling the class method directly (not via an instance), but passing it a reference to the instance in the method call - which makes this snippet equivalent to the one above it.

     
     
    >>> More Python Articles          >>> More By icarus, (c) Melonfire
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek