Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Unconventional Uses of Python Objects

DZone's Guide to

Unconventional Uses of Python Objects

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

One of the greatest features of Python is its flexibility.  It allows for its objects (classes and instances of classes) to be used in a myriad of ways, many of which are not conventionally object-oriented.  Chris Siebenmann has created a short list of ways that he uses Python's objects in this manner:

  • Creating inheritance hierarchies for exceptions so I can have hierarchies of general and specific exceptions. I'm pretty sure that I'm usually over-designing when I do this and that I rarely need an actual hierarchy as opposed to one or two separate exceptions.
  • As data records, ie the equivalent of C structs, in situations where just a list or a dictionary isn't enough. This 'bag of data' design pattern probably horrifies real OO people.
  • For polymorphism, when I have generic operations to apply to a variety of underlying object types. Usually I don't use much or any inheritance for this, because in Python you don't need to use inheritance to get polymorphism; you can just duck type. (This is a superset of objects as data records.)

  • For combining data and behavior together when it seems to make things simpler. My ideal is when all of the code that has to know about internal data storage details can be most conveniently put in one place (ie on the class), but I am willing to put methods on the side of a bag of data when it makes dealing with the data simpler. (Sometimes this involves polymorphism.)
-- Chris Siebenmann

Chris points out that this is not a complete list, but rather a list of the ones he uses most often.  Are there any other ways that you use Python objects that are unconventional?  Please share them in the comments section below.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}