<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Marc Mauri Alloza on Medium]]></title>
        <description><![CDATA[Stories by Marc Mauri Alloza on Medium]]></description>
        <link>https://medium.com/@marcmaurialloza?source=rss-8a7e7a587e04------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*2N1-lolREgT434gb</url>
            <title>Stories by Marc Mauri Alloza on Medium</title>
            <link>https://medium.com/@marcmaurialloza?source=rss-8a7e7a587e04------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 08:36:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marcmaurialloza/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Dependency injection with partials in Python]]></title>
            <link>https://medium.com/@marcmaurialloza/dependency-injection-with-partials-on-init-py-647d8528841e?source=rss-8a7e7a587e04------2</link>
            <guid isPermaLink="false">https://medium.com/p/647d8528841e</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Marc Mauri Alloza]]></dc:creator>
            <pubDate>Fri, 03 Oct 2025 16:22:51 GMT</pubDate>
            <atom:updated>2025-10-17T09:24:46.882Z</atom:updated>
            <content:encoded><![CDATA[<p>Do you know you can provide dependencies on __init__.py so the clients of the package won’t have to deal with that and we also don’t need to hardwire them in our internals?</p><p>That’s exactly what we’re going to do!</p><pre># entity_service/application/service.py</pre><pre>def service(input, entity_repository):<br>    return entity_repository.get_entity(input)<br></pre><pre># entity_service/__init__.py</pre><pre>from functools import partial<br>from entity_service.application import service<br>from entity_service.infrastructure.repositories import entity_repository</pre><pre>service = partial(service.service, entity_repository=entity_repository)</pre><pre>__all__ = [service]</pre><pre># client.py</pre><pre>from entity_service import service</pre><pre>def call_service(input):<br>  service.service(input)</pre><p>The functools.partial function returns a function that doesn’t have the entity_repository argument but keeps all the other arguments and that will call the original function with the entity_repository. The service will not have a direct dependency (import) on infrastructure code anymore.</p><p>In a single line we can define the dependencies of each function or class. I must give credit to this trick to the <a href="https://www.cosmicpython.com/book/chapter_13_dependency_injection.html#_preparing_handlers_manual_di_with_closures_and_partials">Cosmic Python web book</a> that contains many other interesting ideas to structure your Python application.</p><p>We may consider our __init__.py infrastructure or even move the partial to an entrypoint.py file on infrastructure and import from there.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=647d8528841e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Visibility in Python classes]]></title>
            <link>https://medium.com/@marcmaurialloza/visibility-in-python-classes-61ab9e156625?source=rss-8a7e7a587e04------2</link>
            <guid isPermaLink="false">https://medium.com/p/61ab9e156625</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[visibility-modifier]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[visibility]]></category>
            <dc:creator><![CDATA[Marc Mauri Alloza]]></dc:creator>
            <pubDate>Wed, 04 Oct 2023 19:47:51 GMT</pubDate>
            <atom:updated>2023-10-05T10:30:32.300Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re a 90s or 00s developer classes are probably your main way of thinking when programming. Because the popularity of Java we tend to borrow its idioms and even it’s quirks into Python. We’ve become addicts to the “dot notation” that lets us easily discover what a module or a class has to offer to us and also, to define properties with getters and setters and basically to model everything around classes and objects. That wouldn’t be a problem at all if it wasn’t distracting us from simpler alternatives like modules.</p><p>Python classes are declared inside modules. Classes are a way to group related data and functions, especially the ones that repeat much more and operate to a repeated piece of data that keeps application transient internal state.</p><p>Imagine our previous example, the repository must return a domain entity.<br>As we will need many instances of what we’re getting in the repository we choose to implement it with a class.</p><pre>class Entity:<br>  def __init__(self):<br>    self.__private_var = 0<br>    self._protected_var = 0<br>  <br>  def __private_method(self):<br>    print(&quot;Entity __private_method&quot;)<br><br>  def _protected_method(self):<br>    print(&quot;Entity __protected_method&quot;)<br>  <br>  def public_methodA(self):<br>    print(&quot;Entity public_methodA&quot;)<br>  <br>  def public_methodB(self):<br>    self.public_methodA()<br><br>e = Entity()<br>e.public_methodB()<br># output: Entity public_methodA</pre><p>In this case for any instance we have access to this private and protected methods and variables but the underscores indicate that you shouldn’t be touching or overriding that. Also if you’re seeing this code you’re probably a close collaborator on this system and should know why this is done this way and should have a really good reason to call or use that.</p><p>Classes require a new visibility prefix because they allow for extension (inheritance). Extending a class in Python means looking on parent classes for methods and members when they’re called, that is called dynamic resolution. Private methods are mangled (prefixed with the class name) so methods in our class can call to that specific local implementation instead of an override if it gets extended.</p><p>In our later example we had a class where a method called another of their methods. That means someone can take our class and define something like this:</p><pre>class SubEntity(Entity):<br>  def public_methodA(self):<br>    print(&quot;Sub entity public_methodA&quot;)<br><br>sube = SubEntity()<br>sube.public_methodB()<br># output: Sub entity public_method</pre><p>This method will be calling the superclass public method B and that one will be calling the overridden class method. Depending on what are we trying to model we might not be interested on public_methodB being able to be hijacked, especially if that would make out method vulnerable to accidentally strengthen the preconditions of the B method or weaken the post-conditions as that would make our class open for extenders to violate Liskov Substitution Principle.</p><p>However if we implement like this:</p><pre>class Entity:<br>  def __init__(self):<br>    self.__private_var = 0<br>    self._protected_var = 0<br>  <br>  def __private_method(self):<br>    print(&quot;Entity __private_method&quot;)<br><br>  def _protected_method(self):<br>    print(&quot;Entity __protected_method&quot;)<br>  <br>  def public_methodA(self):<br>    print(&quot;Entity public_methodA&quot;)<br>  <br>  def public_methodB(self):<br>    self.__private_method()<br><br>class SubEntity(Entity):<br>  def __private_method(self):<br>    print(&quot;Sub entity __private_method&quot;)<br><br>sube = SubEntity()<br>sube.public_methodB()<br># output: Entity __private_method</pre><p>To check how the mangling works we can use the dir() function on the object to know what things we can do with it.</p><pre>print(dir(sube))<br><br># output: [&#39;_Entity__private_method&#39;, &#39;_Entity__private_var&#39;,<br># &#39;_SubEntity__private_method&#39;, &#39;__class__&#39;, &#39;__delattr__&#39;, &#39;__dict__&#39;,<br># &#39;__dir__&#39;, &#39;__doc__&#39;, &#39;__eq__&#39;, &#39;__format__&#39;, &#39;__ge__&#39;, &#39;__getattribute__&#39;,<br># &#39;__gt__&#39;, &#39;__hash__&#39;, &#39;__init__&#39;, &#39;__init_subclass__&#39;, &#39;__le__&#39;, &#39;__lt__&#39;,<br># &#39;__module__&#39;, &#39;__ne__&#39;, &#39;__new__&#39;, &#39;__reduce__&#39;, &#39;__reduce_ex__&#39;, &#39;__repr__&#39;,<br># &#39;__setattr__&#39;, &#39;__sizeof__&#39;, &#39;__str__&#39;, &#39;__subclasshook__&#39;, &#39;__weakref__&#39;,<br># &#39;_protected_method&#39;, &#39;_protected_var&#39;, &#39;public_methodA&#39;, &#39;public_methodB&#39;]</pre><p>So if we implemented:</p><pre>class SubEntity(Entity):<br>  def _Entity__private_method(self):<br>    print(&quot;Sub entity __private_method&quot;)<br><br>sube = SubEntity()<br>print(dir(sube))<br>sube.public_methodB()<br># output: Sub entity __private_method</pre><p>We would be effectively hijacking the class anyway, of course this makes the code dependent on language specifications that may change so that’s something you shouldn’t be doing.</p><p>And to this point you’ll say: “See, I told you, visibility on Python is a joke”. However many languages have reflection APIs (Python also have, and meta programming also) where you can also access and modify existing methods, they only have more complex APIs to do it which is not that good if you think about it.</p><p>Also we haven’t got to the “quid” of the question that is. How can we really protect our software from unintended usage? We will see that in next posts in the series.</p><h4>Addendum: Classes vs. modules? No! Classes and modules!</h4><p>Due to the dynamic nature of Python we can mix change modules for classes and as long they provide the same interface they will be perfectly interchangeable.</p><p>Let’s revisit our service:</p><pre># service.py<br><br>from service.infrastructure.repositories import entity_repository as sql_alchemy_entity_repository<br>def service(input, entity_repository=sql_alchemy_entity_repository):<br>    return entity_repository.get_entity(input)</pre><p>I we had developed our repository as a class we could instead do:</p><pre># service.py<br><br>from service.infrastructure.repositories import SQLAlchemyEntityRepository<br>def service(input, entity_repository=SQLAlchemyEntityRepository()):<br>    return entity_repository.get_entity(input)</pre><p>See? Classes or modules it doesn’t matter to our users but it does matter as developers to choose the most simple tool at hand to don’t over-complicate our code.</p><p>We can think of modules as single instance classes that can’t be extended. Does Singleton ring a bell?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61ab9e156625" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Demystifying visibility on Python]]></title>
            <link>https://medium.com/@marcmaurialloza/demystifying-visibility-on-python-4200f49a84c4?source=rss-8a7e7a587e04------2</link>
            <guid isPermaLink="false">https://medium.com/p/4200f49a84c4</guid>
            <category><![CDATA[software-design]]></category>
            <category><![CDATA[visibility-modifier]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Marc Mauri Alloza]]></dc:creator>
            <pubDate>Fri, 15 Sep 2023 15:53:11 GMT</pubDate>
            <atom:updated>2023-09-15T15:53:11.825Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re really sure that visibility on Python is just about adding leading underscores and that it isn’t enforced at all by the language you’ll probably learn a couple of surprising things in this article series.</p><h3>Why is visibility so important?</h3><p>Being object oriented and modular programming some of the most popular programming paradigms it is normal that every language has provided tools to support the design principles of this paradigms.</p><p>Let’s review the object oriented design principles and see how related they are to visibility:</p><ol><li>Open Closed Principle: “Things should be open for extension and closed for modification&quot;.</li><li>Interface Segregation Principle: “Clients should not be forced to depend on interfaces they do not use&quot;</li><li>Dependency Inversion Principle: “High level modules shouldn’t depend on low level modules both should depend on abstractions&quot;</li><li>Liskov Substitution Principle: “Objects of a superclass should be replaceable by objects of a subclass”</li></ol><p>See how Liskov Substitution Principle doesn’t look that related? That’s mainly because visibility (or information hiding, or encapsulation) is a concept that was taken from modular programming, a much older style than object oriented programming.</p><p>Being able to control if something (a module, variable or function, a class member or property) is visible or not for clients helps avoid clients use or modify the behaviour of the software component in unintended ways.</p><p>Tools provided by the language to manage visibility allow us to clearly define what things are fine for our clients to touch and what things aren’t providing fine grained interfaces for them to work with out software component.</p><p>Also tools provided by the language to deal with visibility help us abstract our clients from details allowing client to have a more abstract interaction with their dependencies.</p><p>Wrapping up, tools related to visibility help us write software that is easier to change because we hide things that change more often from more stable things.</p><h3>So I’ve been told Python visibility is a joke …</h3><p>It’s fine to play at flame-wars now and then and make fun of other languages specially if the syntax is weird to us and we’re unfamiliar with it. However if we don’t overcome that phase we’re limiting ourselves of learning other perfectly valid ways to think about and approach the same issues.</p><p>In this post series we’ll review what tools Python provides to deal with visibility. Remember the only reason we care about visibility is to apply design principles that allow us to write more usable code so any way the language achieves those goals should be fine isn’t it?</p><p>We’ll review tools that provide for each different software entity.</p><h4>Modules</h4><p>With object oriented programming (class oriented in fact) we’re focusing a lot on applying the principles to class design. However Python also provide modules as a simpler building block. Avoiding the overuse of classes simplifies our code and being able to apply visibility on modules will help us leverage them to their full potential.</p><p>Modules are one the oldest way of organizing code. Modules may contain constants, variables, functions and even classes. Every Python file is considered a module. Each module has its own namespace that contains defined or imported symbols that may refer to other modules, variables, functions or classes. This are the ones you’re allowed to use when writing code inside a file / module. In Python almost everything is an object so it shouldn’t be surprising modules are also objects. The ways to access a module in Python that make more sense are either by importing it and accessing its symbols with the “dot” operator or by directly importing variables and functions from the module into your module.</p><p>Imagine we have an application service. We usually don’t require many instances of a service so it would makes sense to make it a module. If you come from a Java background you may be wondering how we’ll get our dependencies injected. I must ask you to have some patience with this poor Pythonist until the end of the series.</p><pre># service.py<br><br>from service.infrastructure.repositories import entity_repository<br><br>def get_entity_service(entity_id):<br>    return entity_repository.get_entity(entity_id)<br></pre><p>Here we’re creating a service that imports another module, a repository. If you’re a clean architecture bigot you probably won’t like that. I must ask you again to have patience with this poor Pythonist. However, I’ll give you some food for thought, although I am actually importing from infrastructure the interface of the repository doesn’t expose any technicality, so, if I had to change implementation I’d just had to change the import and everything would work as that module will do its own imports we don’t need to be passing its dependencies and as we’re not creating a new object of a specific type we’re protected of getting more hardly coupled to an implementation. I know however is hard-wiring dependencies and we’ll see a way of avoiding that at the end of the article.</p><p>So we need to introduce some authentication logic into this service. We don’t want other code be calling authentication so we prefix it with an underscore signaling it is not intended to be used from out of the module.</p><pre># service.py<br><br>from service.infrastructure.repositories import entity_repository<br>from service.infrastructure import authentication<br><br><br>class AuthenticationError(Exception):<br>   pass<br><br>def _authenticate(user):<br>   if not authentication.authenticate(user):<br>        raise AuthenticationError()<br><br>def service(input, user):<br>    _authenticate(user)<br>    return entity_repository.get_entity(input)<br></pre><p>That underscore is telling users of this module “please don’t use this&quot;. Everything that is just prefixed with a underscore means that in Python. But in reality if visibility is well implemented you shouldn’t see a lot of this unless you made this code yourself or one close colleague and you’re working on the very same package. We’ll see how is that achieved in next posts of this series.</p><p>Although wildcard imports are not endorsed, as they’re not explicit enough, and that would go against <a href="https://peps.python.org/pep-0020/">The Zen Of Python</a> symbols prefixed with underscore are not imported by wildcard imports although they’re still available to access with the “dot notation”.</p><p>In next posts we’ll see how to implement visibility in classes and packages and how to expose safe interfaces to our clients.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4200f49a84c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>