Configuring an object via a dictionary

Pokemon Chw pokemonchw at gmail.com
Mon Mar 18 22:48:02 EDT 2024


It's too complicated, there's no need for this
    def __init__(self, config):

        self.__dict__ = config
        self.connection = None
        """
        other code .....
        """
Note that you need to keep the fields in the config dict named the same as
the fields you want to be assigned to in your class

Loris Bennett via Python-list <python-list at python.org> 于2024年3月19日周二
01:39写道:

> Tobiah <toby at tobiah.org> writes:
>
> > I should mention that I wanted to answer your question,
> > but I wouldn't actually do this.  I'd rather opt for
> > your self.config = config solution.  The config options
> > should have their own namespace.
> >
> > I don't mind at all referencing foo.config['option'],
> > or you could make foo.config an object by itself so
> > you can do foo.config.option.  You'd fill it's attributes
> > in the same way I suggested for your main object.
>
> Thanks for the thoughts.  I'll go for self.config = config after
> all, since, as you say, the clutter caused by the referencing is not
> that significant.
>
> Cheers,
>
> Loris
>
> --
> This signature is currently under constuction.
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list