Home arrow Python arrow Page 5 - Object Oriented Programming With Python (part 1)

Self-Involved - Python

As a true object-oriented language, Python is a great place tostart learning about OO programming. In this first segment of a two-partarticle, take your Python skills to the next level with a discussion ofclasses and class instances. Practical (and not-so-practical) examplesincluded.

TABLE OF CONTENTS:
  1. Object Oriented Programming With Python (part 1)
  2. A Very Big Snake
  3. What's In A Name?
  4. Digging Deep
  5. Self-Involved
  6. Under Construction
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 44
November 01, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- 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

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: