Home arrow Python arrow Page 3 - Object Orientation in Python

Classes in Python - Python

Python is a very object-oriented language, which makes it easy to work with. For those of you who may not be familiar with the concept of object-oriented programming languages, Peyton provides a complete, easy-to-understand explanation, and then gives a number of examples that illustrate how object orientation works in Python.

TABLE OF CONTENTS:
  1. Object Orientation in Python
  2. Another Way to Understand Object Orientation
  3. Classes in Python
  4. More About Creating Classes
By: Peyton McCullough
Rating: starstarstarstarstar / 39
February 14, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

 

Now that you have an understanding of what object orientation is, we'll move on to Python. The first logical step is to create a class. You can define a class pretty much anywhere, including inside conditional statements. Defining a class is simple:

 

class SomeClass:

 

The next step is to add a bit of data to the class. There isn't anything special or unique about this step:

 

class SomeClass:

  "Just some class...that's all."

  someNumber = 127 + 1 / 360 * 2 + 1 / 2

  someString = 'Star Office 7'

 

You can also add methods. These are defined just like functions, except they take self as their first argument:

 

class SomeClass

  "Just some class...that's all."

  someNumber 127 + 1 / 360 * 2 + 1 / 2

  someString = 'Star Office 7'

  def someFunction ( self ):

     return someString + ' tastes like chicken.'

  def someOtherFunction ( self ):

     return False

 

As you can see, it's pretty simple to create a class with data and methods.

 

Next, we must create an instance of our class, or an object derived from it. This is extremely simple and brief:

 

someObject = SomeClass()

 

We can now access the variables defined in our class:

 

someObject.someNumber

someObject.someString

 

Calling the methods defined in our class is also very simple:

 

someObject.someFunction()

someObject.someOtherFunction()

 

It is also possible to define attributes after we have created the object:

 

someObject.someRandomVariable = 5

 

Similarly, it is possible to clean up what we create:

 

del someObject.someRandomVariable

 

Let's say we want to define a variable when we first create an instance of our class. This can be accomplished by creating a special method of our class, __init__. The method will be executed when our class is first created.

 

class AnotherClass:

  def __init__ ( self, anotherArgument ):

      self.anotherArgument = anotherArgument

 

anotherObject = AnotherClass ( 'Hello object-oriented world.' )

print anotherObject.anotherArgument # "Hello object-oriented world."

 



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

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: