DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Unconventional Uses of Python Objects

Unconventional Uses of Python Objects

Chris Smith user avatar by
Chris Smith
·
Mar. 20, 12 · Interview
Like (0)
Save
Tweet
Share
3.47K Views

Join the DZone community and get the full member experience.

Join For Free
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.
Object (computer science) Python (language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Create a CLI Chatbot With the ChatGPT API and Node.js
  • How Elasticsearch Works
  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling
  • Create Spider Chart With ReactJS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: