Comments (10)
I agree with @sitnarf comments. I believe the IO layer (i.e., a dict from JSON or similar) should be clearly decoupled from the core data model of the class. One possible mechanism to do this is using staticmethod
or a class method.
For example.
class Menu:
def __init__(self, title:str, body:str):
self.title = title
self.body = body
@staticmethod
def from_dict(d:dict):
return Menu(d['title'], d['body'])
def example():
# perhaps loaded from JSON or defined as code
# in your app
raw_conf = dict(title="Title", body="Content")
m1 = Menu.from_dict(raw_conf)
# if you "trust" the source and don't want to
# explicitly write a "from_dict" method
# then you can pass the dict.
m2 = Menu(**raw_conf)
return m1, m2
from clean-code-python.
Yeah, this seems like a weirdly Java-y thing to recommend in Python! Especially when you have kwargs...
from clean-code-python.
I also think that the comparison is honestly not equivalent. A function is getting X number of arguments where as the constructor gets a single dictionary. A function can work just as well with the dictionary 😄
from clean-code-python.
Unpopular opinion: using "no-schema" dictionaries with key access/validation/assertion in constructor is similar with using no-sql databases: technically, you still have a contract (or scheme), but implicit. I usually use pure python dict subclasses with fixed key sets (or dataclasses in new python versions). Errors will be more detailed with TypeError on creating MyDictyType object than on construction call of an another object.
If your data has a schema, use a structure to keep it
You may be tempted to use a list (or tuple, if your language allows) to keep your data if it's simple -- like, say, only 2 fields.
But if you data has a schema -- it has a fixed format -- you should always use some structure to keep it, but it a struct or a class.
(https://blog.juliobiason.me/thoughts/things-i-learnt-the-hard-way/)
from clean-code-python.
I agree with @sitnarf comments. I believe the IO layer (i.e., a dict from JSON or similar) should be clearly decoupled from the core data model of the class. One possible mechanism to do this is using
staticmethod
or a class method.For example.
class Menu: def __init__(self, title:str, body:str): self.title = title self.body = body @staticmethod def from_dict(d:dict): return Menu(d['title'], d['body']) def example(): # perhaps loaded from JSON or defined as code # in your app raw_conf = dict(title="Title", body="Content") m1 = Menu.from_dict(raw_conf) # if you "trust" the source and don't want to # explicitly write a "from_dict" method # then you can pass the dict. m2 = Menu(**raw_conf) return m1, m2
May be better to use @classmethod
here? Explanation
from clean-code-python.
Thanks. These are all good suggestions. I've addressed them in d08459e. I'm closing this issue for now. Sorry it took so long.
from clean-code-python.
@zedr no problem, thank you for addressing the issue! 😉
from clean-code-python.
@zedr However, your code is not completely correct. See https://mypy-play.net/?mypy=latest&python=3.8&gist=4a366a0393a1341976132d00ef52f02e. Also, see https://www.python.org/dev/peps/pep-0589/#rejected-alternatives.
from clean-code-python.
TypedDict unfortunately lacks default values. The problem can be mitigated by using @dataclass
, but from my experience, using TypedDict is better, since it is much more clear and you can do syntax tricks like {**foo_dict, **bar_dict}
.
One way how to mitigate the lack of default values would be to use factory functions:
def make_menu_config(
title: str, body: str,
button_text: str,
cancellable: bool = False
) -> MenuConfig:
return MenuConfig(*args, **kwargs)
from clean-code-python.
TypedDict unfortunately lacks default values. The problem can be mitigated by using
@dataclass
, but from my experience, using TypedDict is better, since it is much more clear and you can do syntax tricks like{**foo_dict, **bar_dict}
.One way how to mitigate the lack of default values would be to use factory functions:
def make_menu_config( title: str, body: str, button_text: str, cancellable: bool = False ) -> MenuConfig: return MenuConfig(*args, **kwargs)
IMO best alternative for everything :) - attrs
lib. https://www.attrs.org/en/stable/why.html
from clean-code-python.
Related Issues (14)
- Clean code for Python
- Possibly incorrect: "Zero arguments is the ideal case" HOT 1
- Please give us more :) HOT 3
- Translation - PT-BR HOT 5
- Using `str` instead of `Text` HOT 2
- Translated to Persian language HOT 7
- Request: French translation
- Translate to Chinese HOT 1
- Many links are not working HOT 1
- Translate to Korean HOT 1
- the DIP example seems wrong to me
- In
- MenuConfig example mutating/setting class variables listed as Good HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clean-code-python.