Home arrow Python arrow Page 4 - Object Orientation in Python

More About Creating Classes - 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

 

I mentioned subclasses and inheritance in the previous section. We will now take a look at how to create a class that derives its methods and variables from another class:

 

class ParentClass:

  testVariableOne = 'This is a test. I promise.'

  testVariableTwo = 'No, really, it is just a test.'

  def __init__ ( self, testArgument ):

     self.testVariableThree = testArgument

  def testMethod ( self ):

     pass

 

class ChildClass ( ParentClass ):

  pass

 

parentObject = ParentClass ( 'Hello, how are you?' )

print parentObject.testVariableOne # "This is a test. I promise."

print parentObject.testVariableTwo # "No, really, it is just a test."

print parentObject.testVariableThree # "Hello, how are you?"

 

childObject = ChildClass ( 'I am fine, thank you.' )

print childObject.testVariableOne # "This is a test. I promise."

print childObject.testVariableTwo # "No, really, it is just a test."

print childObject.testVariableThree # "I am fine, thank you."

 

Subclasses are perfectly capable of defining their own variables and methods, too. They can also overwrite variables and methods passed down from the original class:

 

class ParentClass:

  testVariableA = 'This is a test variable. Pay no attention to it.'

  testVariableB = 'This is also a test variable. Or is it? We may never know.'

  def __init__ ( self, testArgument ):

     self.testVariableC = testArgument

  def testMethod ( self ):

     return False

 

class ChildClass ( ParentClass ):

  testVariableB = 'I am a very rebellious variable. Yes I am.'

  def testMethod ( self ):

     return True

 

parentObject = ParentClass ( 'I am an insignificant test argument.' )

print parentObject.testVariableA # "This is a test variable. Pay no attention to it."

print parentObject.testVariableB # "This is a test variable. Or is it? We may never know."

print parentObject.testVariableC # "I am an insignificant test argument"

print parentObject.testMethod() # False

 

childObject = ChildClass ( 'This is a subclass of ParentClass.' )

print childObject.testVariableA # "This is a test variable. Pay no attention to it."

print childObject.testVariableB # "I am a very rebellious variable. Yes I am."

print childObject.testVariableC # "This is a subclass of Parent Class."

print childObject.testMethod() # True

 

If you only want to add to a parent's method, this is also possible by calling the class's method and supplying the needed arguments:

 

class ParentClass:

  def testMethod ( self, testArgument ):

     return str ( len ( testArgument ) )

class ChildClass:

  def testMethod ( self, testArgument ):

     return ParentClass.testMethod ( self, testArgument ) + ' ( Extra Stuff )'

 

parentObject = ParentClass()

print parentObject.testMethod ( 'This is a long string.' ) # "22"

 

childObject =ChildClass()

print childObject.testMethod ( 'This is a long string.' ) # "22 ( Extra Stuff )"

 

It is also possible to create a subclass with multiple parents:

 

class ParentClassOne:

  testVariableA = "This is another test. In fact, this is the last one."

 

class ParentClassTwo:

  testVariableB = "Caffeine fuels the world."

  def testMethodA ( self ):

     return True

 

class ParentClassThree:

  def testMethodB ( self ):

     return False

 

class ChildClass ( ParentClassOne, ParentClassTwo, ParentClassThree ):

  pass

 

anObject = ChildClass()

print anObject.testVariableA # "This is another test. In fact, this is the last one."

print anObject.testVariableB # "Caffeine fuels the world."

print anObject.testMethodA() # True

print anObject.testMethodB() # False

 

Conclusion

 

As you've witnessed, Python itself is unbelievably object-oriented, and it supports both simple and complex object-oriented design. While scripting in Python, you will come across object orientation regularly. When you create a string, you are creating an object. When you work with files, you are working with objects as well. When you work with Python's extensive standard library, you are, again, working with objects. Objects make Python an incredibly easy language to work with.

 

You now understand the basics of objects and object-oriented programming. You may now utilize your knowledge in your scripts, potentially simplifying the development process.



 
 
>>> 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: