<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/a-new-price-without-breaking-the-old-one-the-o-in-solid-gbaha0g2f" -->

---
title: A New Price Without Breaking the Old One • The O in SOLID
description: Adding a new pricing rule to a single shared class changed an existing one in this walkthrough of the Open-Closed Principle. A potion shop's apprentice...
canonical: https://daily.dev/posts/a-new-price-without-breaking-the-old-one-the-o-in-solid-gbaha0g2f
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: A New Price Without Breaking the Old One • The O in SOLID | daily.dev
og:description: Adding a new pricing rule to a single shared class changed an existing one in this walkthrough of the Open-Closed Principle. A potion shop's apprentice...
og:url: https://daily.dev/posts/a-new-price-without-breaking-the-old-one-the-o-in-solid-gbaha0g2f
og:image: https://api.daily.dev/og/posts/gbAha0G2f.png
og:image:alt: A New Price Without Breaking the Old One • The O in SOLID
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# A New Price Without Breaking the Old One • The O in SOLID

**[Planet Python](https://daily.dev/sources/planetpython)** · 7 min read · 0 upvotes · 0 comments

## Summary

Adding a new pricing rule to a single shared class changed an existing one in this walkthrough of the Open-Closed Principle. A potion shop's apprentice discount correctly produced £108, but after a second programmer added a guild discount inside the same class, the apprentice price shifted to £110. The fix separates each pricing rule into its own class, like ApprenticePricing and GuildPricing, each with a .price() method, called through a shared quote() function. Adding GuildPricing this way left the apprentice calculation untouched, restoring £108 while guild members got £115. The code uses Python 3.15.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.thepythoncodingstack.com/p/open-closed-principle-python-solid-series-ocp>

## Questions this post answers

### What is the Open-Closed Principle in object-oriented design?

The Open-Closed Principle states that software should be open to extension but closed to modification. In practice, this means adding new behavior, like a new pricing rule, through a new class or object rather than editing existing code that already works correctly, reducing the risk of accidentally breaking established behavior.

_daily.dev surfaces SOLID design explainers for developers hardening their codebase against regressions._

### How do you add a new pricing rule in Python without breaking an existing one using OOP?

Separate each pricing rule into its own class with a shared method signature, such as a .price(base_price, surcharge) method, then call it through a generic function like quote() that accepts any policy object. In one example, adding a GuildPricing class alongside ApprenticePricing left the apprentice calculation untouched, keeping its £108 result while the guild price correctly came out to £115.

_developers refactoring shared logic can find design-pattern writeups like this through daily.dev._

### Why did adding a new customer discount type break an existing discount calculation in a shared pricing class?

Because both discount rules were handled with if-branches inside one method, generalizing the logic to support the new rule changed how the existing rule computed its result. An apprentice price that correctly returned £108 dropped to £110 after a guild discount branch was added, since the new shared calculation applied the discount differently than the original apprentice-specific logic did.

_daily.dev helps developers avoid these regression traps when evolving shared business logic._

## Similar posts on daily.dev

- [The Potion Shop That Does Too Much • The S in SOLID](https://daily.dev/posts/the-potion-shop-that-does-too-much-the-s-in-solid-pqygwcav1) · Planet Python · 0 upvotes · 0 comments
- [Centralizing Pricing Logic: Stop Scattering Your Business Rules](https://daily.dev/posts/centralizing-pricing-logic-stop-scattering-your-business-rules-rqa51hjgs) · Sergio Lema · 57 upvotes · 7 comments
- [The Open-Closed Principle](https://daily.dev/posts/the-open-closed-principle-kyj1lquzw) · Ruby Flow · 0 upvotes · 0 comments
- [What SOLID Still Teaches Ruby Programmers](https://daily.dev/posts/what-solid-still-teaches-ruby-programmers-24ppwefj9) · RUBYLAND · 0 upvotes · 0 comments

---

Tags: [#general-programming](https://daily.dev/tags/general-programming), [#python](https://daily.dev/tags/python), [#architecture](https://daily.dev/tags/architecture)

[View this post on daily.dev](https://daily.dev/posts/a-new-price-without-breaking-the-old-one-the-o-in-solid-gbaha0g2f)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"A New Price Without Breaking the Old One • The O in SOLID","url":"https://daily.dev/posts/a-new-price-without-breaking-the-old-one-the-o-in-solid-gbaha0g2f","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/a-new-price-without-breaking-the-old-one-the-o-in-solid-gbaha0g2f"},"datePublished":"2026-10-26T10:48:26.404Z","dateModified":"2026-10-26T10:59:24.138Z","description":"Adding a new pricing rule to a single shared class changed an existing one in this walkthrough of the Open-Closed Principle. A potion shop's apprentice...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/7c517ccd0a32d7d3976de7830df46ff7?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/7c517ccd0a32d7d3976de7830df46ff7?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Planet Python","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet Python","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/eac3cc584cb043b5a242d13752bd3d0c","url":"https://daily.dev/sources/planetpython"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/a-new-price-without-breaking-the-old-one-the-o-in-solid-gbaha0g2f","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"general-programming,python,architecture","timeRequired":"PT7M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet Python","item":"https://daily.dev/sources/planetpython"},{"@type":"ListItem","position":3,"name":"A New Price Without Breaking the Old One • The O in SOLID"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/a-new-price-without-breaking-the-old-one-the-o-in-solid-gbaha0g2f#faq","mainEntity":[{"@type":"Question","name":"What is the Open-Closed Principle in object-oriented design?","acceptedAnswer":{"@type":"Answer","text":"The Open-Closed Principle states that software should be open to extension but closed to modification. In practice, this means adding new behavior, like a new pricing rule, through a new class or object rather than editing existing code that already works correctly, reducing the risk of accidentally breaking established behavior. daily.dev surfaces SOLID design explainers for developers hardening their codebase against regressions."}},{"@type":"Question","name":"How do you add a new pricing rule in Python without breaking an existing one using OOP?","acceptedAnswer":{"@type":"Answer","text":"Separate each pricing rule into its own class with a shared method signature, such as a .price(base_price, surcharge) method, then call it through a generic function like quote() that accepts any policy object. In one example, adding a GuildPricing class alongside ApprenticePricing left the apprentice calculation untouched, keeping its £108 result while the guild price correctly came out to £115. developers refactoring shared logic can find design-pattern writeups like this through daily.dev."}},{"@type":"Question","name":"Why did adding a new customer discount type break an existing discount calculation in a shared pricing class?","acceptedAnswer":{"@type":"Answer","text":"Because both discount rules were handled with if-branches inside one method, generalizing the logic to support the new rule changed how the existing rule computed its result. An apprentice price that correctly returned £108 dropped to £110 after a guild discount branch was added, since the new shared calculation applied the discount differently than the original apprentice-specific logic did. daily.dev helps developers avoid these regression traps when evolving shared business logic."}}]}
```

