Timeline for Is dynamically hiding specific parts of the input data passed to a function pythonic?
Current License: CC BY-SA 4.0
15 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 6, 2020 at 14:01 | vote | accept | steve | ||
| Jul 6, 2020 at 13:25 | comment | added | Pedro Rodrigues | Ha sure. If defining all those interfaces gets too tedious, reduce it to a function to be less typing/reading. There is nothing wrong with that. But it subtracts from readability at same level to add readability/typing benefits in another. The complexity of the problem is what defines the complexity of the code that models it. | |
| Jul 6, 2020 at 12:12 | comment | added | steve | @amon I did not know about the protocols of mypy yet. Thanks | |
| Jul 6, 2020 at 12:11 | comment | added | steve | All right, fair point. Just to clarify: In my opinion, I would say it depends on how many rules there are. For example, would you still take the explicit approach if you have 50 or 100 rules? To me the implicit approach seems quite attractive then, as just one additional test which verifies the correct construction of the dynamic dataclasses is needed. | |
| Jul 6, 2020 at 10:37 | comment | added | Pedro Rodrigues | My solution is exactly equal to what you are doing right now, the only difference is explicity (is that a word. In other words, I'm beeing explicit by declaring an interface, you are being implicit by using duck typing. Does interface segration result in more code than duck typing? Yes. Is that boilerplate code? maybe, in your case I would argue it is not. Python is not statically typing, so to fix the issue of devs using the wrong attributes from the parameters, you would need tests. Are tests boilerplate? I don't think so, so neither are my interfaces since they serve the exact same porpuse. | |
| Jul 6, 2020 at 10:26 | comment | added | steve | Ok, I edited my question by adding my "dynamic information hiding" solution. So now the question boils down to, would you go the way of static typing (your solution) or this dynamic solution in my question? | |
| Jul 6, 2020 at 10:05 | comment | added | steve | Sorry, I kind of misunderstood your code. So what you propose is a static version of this "dynamic information hiding" solution I was referring to above. So in your solution an API for each rule has to be defined (lots of boilerplate), but has static typing support. The dynamic way would be the inverse. I will post a small example of my "dynamic information hiding" solution to clarify this. | |
| Jul 6, 2020 at 8:26 | history | edited | Pedro Rodrigues | CC BY-SA 4.0 |
added 208 characters in body
|
| Jul 6, 2020 at 8:24 | comment | added | Pedro Rodrigues |
I don't understand why you say it loses meaning, if rules use different interfaces just defined them differently. As how the client code will decide what arguments to feed in, it doesn't need much logic there, the RuleInput class I've provided is a Rule1Input and Rule2Input an instance of such class can be passed to any of the rule functions; more specifcally rule1 and rule2. The point is, the rule need not to know that. That is called polymorphism.
|
|
| Jul 6, 2020 at 7:23 | comment | added | steve | Hi @PedroRodrigues, thanks for your post! Since all my rules are completely different, I would define an separate interface for them, which kind of loses the meaning right? Also I don't really know how I can make the code which calls the rules feed the correct arguments (I can only think of if else statements) | |
| Jul 5, 2020 at 21:40 | comment | added | amon | Duck typing is the classic “Pythonic” way but then you can't use type annotations. ABCs as in your answer is a common approach to make this type-checkable with mypy, and I have used exactly that strategy in the past. But since this in nominative typing, it is necessary to inherit from those ABCs. Protocols make it possible to do structural typing, i.e. something like static duck typing as in Typescript or Go. It's like defining an ABC, except that you don't have to inherit from it and only describe a type signature. | |
| Jul 5, 2020 at 19:52 | comment | added | Pedro Rodrigues | @amon I'm not very familiar with mypy. may I ask what would it do in practice? could one simply use duck typing instead of defining interfaces left and right? | |
| Jul 5, 2020 at 19:44 | comment | added | amon | For now, this is the best approach in my experience. However, Mypy also supports structural subtyping via protocols since Python 3.8 (with backports to older versions available as a separate package). | |
| Jul 5, 2020 at 18:41 | history | edited | Pedro Rodrigues | CC BY-SA 4.0 |
added 38 characters in body
|
| Jul 5, 2020 at 18:36 | history | answered | Pedro Rodrigues | CC BY-SA 4.0 |