6

SOLID Principles explained in Python with examples.

 1 year ago
source link: https://gist.github.com/dmmeteo/f630fa04c7a79d3c132b9e9e5d037bfd
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Author

dmmeteo commented on May 30, 2019

TODO: need refactoring to normal view

Good job.

Great work, appreciate it!!
Have a doubt with respect to ocp.py, please clarify.
For the same scenario, if I want to get user input to form animals array, how would we let the animal_sound method automatically detect which class to call??
For instance, user provides input as ["Cat", "Snake", "Lion"] for animals array from the terminal during the run-time, how would animal_sound method handle it ?

def animal_sound(animals: list):
    for animal in animals:
         animal = eval((f"{animal}"))()
         print(animal.make_sound())

Loved it. Thanks. Clear and to the point.

awsome!

It's quite helpful. Especially the last one. Thank you a lot.

Well written with clear examples.

Thank you! Good job.

Regarding Dependency Inversion, on line 62 in dip.py, in XMLHttpService, shouldn't an object of type XMLHttpRequest be injected, i.e. passed via a constructor, in order to respect Dependency Injection? I've used a type hint to denote the object's type.

class XMLHttpService(Connection):
    def __init__(self, xhr: XMLHttpRequest):
        super(XMLHttpService, self).__init__()
        self.xhr = xhr

    def request(self, url: str, options:dict):
        self.xhr.open()
        self.xhr.send()

This is really helpful

Well done, sir.
Thank you! <3

Really helpful, Thank you

Great work, appreciate it!!

Alanxu5 commented on Jan 10

This is great. Thanks!

JarroVGIT commented on Mar 23

This is a great overview, thanks for this!

One question though. In the example of OCP, on the final lines (147-149), the get_discount() method will return a price that is 2 time a normal discounted price, not applying the discount twice on the original price?

Great job. I have different opinion about OCP example. I think best way is to create a main class which receive type of discount and then handle that based on type of discount in different classes. Not so much different, but open to more extension and also more easy to follow by developer even without docs!

Great! Thanks

lucanaso commented on Jul 31

Thank you for the post! I enjoyed the ease of reading.
Typo: the last docstring of the first principle does not close, https://gist.github.com/dmmeteo/f630fa04c7a79d3c132b9e9e5d037bfd#file-1-srp-py-L48

vinimf7714 commented on Aug 6

Hi, everybody!

About the example 2.ocp, line 129. To return discount to clients.
let's suppose that, my discount is returning from database, in this scenario, make sense keep the the classes inheriting of the
discount

I think that would working like this:

class Discount:
    def __init__(self, price, discount):
        self.price = price
        self.discount = discount

    def get_discount(self):
            return self.price * self.discount

Please, let me know if you consent with this

Verify Github on Galxe. gid:5z38VRP6FAN4fu3AgMHGuV

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK