Hacker News new | past | comments | ask | show | jobs | submit login

I am curious about how this type of "clean separation" is generally handled in Python. I mostly have experience with Java, and I have some light Python experience but I've never worked in a large Python codebase.

Would you mind telling me how you would handle this in an example?

Let's say you were building a web app which had to communicate with some sort of payment gateway API. Naturally since you are communicating with some third-party API you want to prevent the rest of your codebase from knowing too much about the API - in case you ever need to change gateways - and so you wrap it up in an interface/module/etc to abstract away the details of Gateway X.

If it ever came time to switch payment gateway backends, in a Java application using DI you would just need to switch which implementation of your interface that the rest of your code is wired up with (either in XML, or if you are wiring up collaborators explicitly in your code, etc).

How would you generally manage this type of thing in a Python system? By giving the web/controller code a different payment module that implemented all of the same method signatures?




you want to prevent the rest of your codebase from knowing too much about the API - in case you ever need to change gateways

Maybe I'm naive, but I wouldn't usually bother:

1) Maybe I will never change gateways, then there was no point in writing a wrapper.

2) If I do change gateways, I can just write a wrapper that uses the old gateway's API as the interface for the new one.

If the new gateway is sufficiently different than the old one that it can't be mapped to the old API, then whatever wrapper I wrote back in the beginning before I needed it wouldn't have been sufficient anyway.

Your strategy sounds like YAGNI to me. I only write separate interfaces when I actually have different components that need to be swappable.

But maybe that's just because I'm using Ruby and I can be lazy like that?


"If it ever came time to switch payment gateway backends, in a Java application using DI you would just need to switch which implementation of your interface that the rest of your code is wired up with (either in XML, or if you are wiring up collaborators explicitly in your code, etc)."

You simply pass in the desired payment processor as a parameter. End of story.

This is part of the reason Java is such a terrible language for actually learning about programming. "Using Dependency Injection" is an absurd way of phrasing "pass in varying parameters as parameters to your function instead of using global variables". What should be the normal way of doing business has instead been elevated to Something Special (that Lesser Languages Can't Do because they don't call it Dependency Injection because they would never think of giving Passing Parameters to Functions a special name!) because Java makes something as easy as breathing and in some cases hard to avoid (try programming Haskell or Erlang with too many global variables) a production.

In Java, you use absurd and unnecessary machinery. In every other language, you just do it. It's hard to even explain how you do it because there's hardly any doing in the it. It's a parameter passed in. It hardly seems worth discussion.


For the sake of this example, let's say that the different gateways have vastly different APIs, messaging mechanisms, etc.

So in the code that you are passing a parameter to, you'd have to have some sort of branch based on the parameter value to choose to call Gateway A versus Gateway B - correct?

Which requires you, when switching gateways, to change the value of the parameter passed along with possibly updating the branch to be aware of what the different Gateway modules you might call would be - correct?


This is basic object orientation: You pass in an object that corresponds to the desired interface. "Facade pattern" if you need something to Google, but this is really basic language-agnostic OO stuff.

Another reason to dislike the Java cruft; it successfully hides the simple things going on behind immense machinery and complicated terminology.


So this is what confuses me about claims that in Python you don't need all this extra cruft, because the language makes these things possible:

if you are using the Facade pattern in Python for this example, you'd be using the same pattern in Java.

So then, what exactly is different here?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: