Code Monkey home page Code Monkey logo

Comments (10)

mpkocher avatar mpkocher commented on August 10, 2024 2

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.

takkaria avatar takkaria commented on August 10, 2024 1

Yeah, this seems like a weirdly Java-y thing to recommend in Python! Especially when you have kwargs...

from clean-code-python.

shabie avatar shabie commented on August 10, 2024

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.

z00sts avatar z00sts commented on August 10, 2024

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.

IaroslavR avatar IaroslavR commented on August 10, 2024

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.

zedr avatar zedr commented on August 10, 2024

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.

sitnarf avatar sitnarf commented on August 10, 2024

@zedr no problem, thank you for addressing the issue! 😉

from clean-code-python.

sitnarf avatar sitnarf commented on August 10, 2024

@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.

sitnarf avatar sitnarf commented on August 10, 2024

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.

IaroslavR avatar IaroslavR commented on August 10, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.