Hi,
I am initialising an object via the following:
self.source_name = config['source_name']
Hi,
I am initialising an object via the following:
def __init__(self, config):
self.connection = None
self.source_name = config['source_name']
self.server_host = config['server_host']
self.server_port = config['server_port']
self.user_base = config['user_base']
self.user_identifier = config['user_identifier']
self.group_base = config['group_base']
self.group_identifier = config['group_identifier']
self.owner_base = config['owner_base']
However, some entries in the configuration might be missing. What is
the best way of dealing with this?
I could of course simply test each element of the dictionary before
trying to use. I could also just write
self.config = config
but then addressing the elements will add more clutter to the code.
However, with a view to asking forgiveness rather than
permission, is there some simple way just to assign the dictionary
elements which do in fact exist to self-variables?
Or should I be doing this completely differently?
Hi,
I am initialising an object via the following:
def __init__(self, config):
self.connection = None
self.source_name = config['source_name']
self.server_host = config['server_host']
However, with a view to asking forgiveness rather than
permission, is there some simple way just to assign the dictionary
elements which do in fact exist to self-variables?
On 3/15/2024 5:30 AM, Loris Bennett via Python-list wrote:
Hi,
I am initialising an object via the following:
def __init__(self, config):
self.connection = None
self.source_name = config['source_name']
self.server_host = config['server_host']
self.server_port = config['server_port']
self.user_base = config['user_base']
self.user_identifier = config['user_identifier']
self.group_base = config['group_base']
self.group_identifier = config['group_identifier']
self.owner_base = config['owner_base']
However, some entries in the configuration might be missing. What is
the best way of dealing with this?
I could of course simply test each element of the dictionary before
trying to use. I could also just write
self.config = config
but then addressing the elements will add more clutter to the code.
However, with a view to asking forgiveness rather than
permission, is there some simple way just to assign the dictionary
elements which do in fact exist to self-variables?
Or should I be doing this completely differently?
self.source_name = config.get('source_name', default_value)
Or, if you like this kind of expression better,
self.source_name = config.get('source_name') or default_value
'default'config = {}
config['source_name'] = ""
config.get('source_name') or 'default'
On 2024-03-15, Thomas Passin via Python-list <python-list@python.org> wrote:
On 3/15/2024 5:30 AM, Loris Bennett via Python-list wrote:
Hi,
I am initialising an object via the following:
def __init__(self, config):
self.connection = None
self.source_name = config['source_name']
self.server_host = config['server_host']
self.server_port = config['server_port']
self.user_base = config['user_base']
self.user_identifier = config['user_identifier']
self.group_base = config['group_base']
self.group_identifier = config['group_identifier']
self.owner_base = config['owner_base']
However, some entries in the configuration might be missing. What is
the best way of dealing with this?
I could of course simply test each element of the dictionary before
trying to use. I could also just write
self.config = config
but then addressing the elements will add more clutter to the code.
However, with a view to asking forgiveness rather than
permission, is there some simple way just to assign the dictionary
elements which do in fact exist to self-variables?
Or should I be doing this completely differently?
self.source_name = config.get('source_name', default_value)
Or, if you like this kind of expression better,
self.source_name = config.get('source_name') or default_value
Won't the latter version misbehave if the value of config['source_name'] has a
"false" boolean value (e.g. "", 0, 0.0, None, [], (), {}, ...)
'default'config = {}
config['source_name'] = ""
config.get('source_name') or 'default'
[...] And I suppose there is always the possibility that sometime in
the future an "or" clause like that will be changed to return a
Boolean, which one would expect anyway.
Hi,
I am initialising an object via the following:
def __init__(self, config):
self.connection = None
self.source_name = config['source_name']
self.server_host = config['server_host']
self.server_port = config['server_port']
self.user_base = config['user_base']
self.user_identifier = config['user_identifier']
self.group_base = config['group_base']
self.group_identifier = config['group_identifier']
self.owner_base = config['owner_base']
However, some entries in the configuration might be missing. What is
the best way of dealing with this?
I could of course simply test each element of the dictionary before
trying to use. I could also just write
self.config = config
but then addressing the elements will add more clutter to the code.
However, with a view to asking forgiveness rather than
permission, is there some simple way just to assign the dictionary
elements which do in fact exist to self-variables?
Or should I be doing this completely differently?
On 2024-03-15 at 15:48:17 -0400,
Thomas Passin via Python-list <python-list@python.org> wrote:
[...] And I suppose there is always the possibility that sometime in
the future an "or" clause like that will be changed to return a
Boolean, which one would expect anyway.
Not only is the current value is way more useful, but changing it would
be a compatibility and maintenance nightmare.
If I want Java, I know where to find it. :-)
[...] And I suppose there is always the possibility that sometime in
the future an "or" clause like that will be changed to return a
Boolean, which one would expect anyway.
On 15 Mar 2024, at 19:51, Thomas Passin via Python-list <python-list@pytho=n.org> wrote:
=20
I've always like writing using the "or" form and have never gotten bit
On 15 Mar 2024, at 19:51, Thomas Passin via Python-list <python-list@python.org> wrote:
I've always like writing using the "or" form and have never gotten bit
I, on the other hand, had to fix a production problem that using “or” introducted.
I avoid this idiom because it fails on falsy values.
Barry via Python-list schreef op 16/03/2024 om 9:15:
Me too. It's just too fragile. When writing code you're going to need an alternative for cases where "config.get('source_name') or default_value" doesn't work correctly; much better to use that alternative for all cases.
On 15 Mar 2024, at 19:51, Thomas Passin via Python-list<python-list@python.org> wrote:
bitI've always like writing using the "or" form and have never gotten
I, on the other hand, had to fix a production problem that using “or” >> introducted.
I avoid this idiom because it fails on falsy values.
On 15 Mar 2024, at 19:51, Thomas Passin via Python-list <python-list@python.org> wrote:
I've always like writing using the "or" form and have never gotten bit
I, on the other hand, had to fix a production problem that using “or” introducted.
I avoid this idiom because it fails on falsy values.
thon.org> wrote:On 15 Mar 2024, at 19:51, Thomas Passin via Python-list <python-list@py=
=9Cor=E2=80=9D introducted.I've always like writing using the "or" form and have never gotten bit=20
I, on the other hand, had to fix a production problem that using =E2=80=
I avoid this idiom because it fails on falsy values.
On 2024-03-16 08:15:19 +0000, Barry via Python-list wrote:
On 15 Mar 2024, at 19:51, Thomas Passin via Python-list <python-list@python.org> wrote:
I've always like writing using the "or" form and have never gotten bit
I, on the other hand, had to fix a production problem that using “or” introducted.
I avoid this idiom because it fails on falsy values.
Perl has a // operator (pronounced "err"), which works like || (or),
except that it tests whether the left side is defined (not None in
Python terms) instead of truthy. This still isn't bulletproof but I've
found it very handy.
On 17/03/24 12:06, Peter J. Holzer via Python-list wrote:t@python.org> wrote:
On 2024-03-16 08:15:19 +0000, Barry via Python-list wrote:
On 15 Mar 2024, at 19:51, Thomas Passin via Python-list <python-lis=
bitI've always like writing using the "or" form and have never gotten =
=80=9Cor=E2=80=9D introducted.=20
I, on the other hand, had to fix a production problem that using =E2=
=20I avoid this idiom because it fails on falsy values.=20
Perl has a // operator (pronounced "err"), which works like || (or),
except that it tests whether the left side is defined (not None in
Python terms) instead of truthy. This still isn't bulletproof but I've found it very handy.
=20
So, if starting from:
=20
def method( self, name=3DNone, ):
=20
rather than:
=20
self.name =3D name if name else default_value
=20
ie
=20
self.name =3D name if name is True else default_value
the more precise:
=20
self.name =3D name if name is not None or default_value
=20
or:
=20
self.name =3D default_value if name is None or name
On 2024-03-17 17:15:32 +1300, dn via Python-list wrote:
On 17/03/24 12:06, Peter J. Holzer via Python-list wrote:
On 2024-03-16 08:15:19 +0000, Barry via Python-list wrote:
On 15 Mar 2024, at 19:51, Thomas Passin via Python-list <python-list@python.org> wrote:I, on the other hand, had to fix a production problem that using “or” introducted.
I've always like writing using the "or" form and have never gotten bit >>>>
I avoid this idiom because it fails on falsy values.
Perl has a // operator (pronounced "err"), which works like || (or),
except that it tests whether the left side is defined (not None in
Python terms) instead of truthy. This still isn't bulletproof but I've
found it very handy.
So, if starting from:
def method( self, name=None, ):
rather than:
self.name = name if name else default_value
ie
self.name = name if name is True else default_value
These two lines don't have the same meaning (for the reason you outlined below). The second line is also not very useful.
the more precise:
self.name = name if name is not None or default_value
or:
self.name = default_value if name is None or name
Those are syntax errors. I think you meant to write "else" instead of
"or".
Yes, exactly. That's the semantic of Perl's // operator.
JavaScript has a ?? operator with similar semantics (slightly
complicated by the fact that JavaScript has two "nullish" values).
'Fred Flintstone'default_value = "default"
name = "Fred Flintstone"
name if name is not None else default_value
'default'name = None
name if name is not None else default_value
Falsename = False
name if name is not None else default_value
1name = 1
name if name is not None else default_value
0name = 0
name if name is not None else default_value
'Fred Flintstone'name = "Fred Flintstone"
default_value if name is None else name
'default'name = None
default_value if name is None else name
Falsename = False
default_value if name is None else name
5unpacked(**mydict)
{'gamma': 7, 'delta': 8}unpacked({"gamma":7, "delta":8})
5alpha
6beta
gamma
On 17/03/24 12:06, Peter J. Holzer via Python-list wrote:<python-list@python.org> wrote:
On 2024-03-16 08:15:19 +0000, Barry via Python-list wrote:
On 15 Mar 2024, at 19:51, Thomas Passin via Python-list =
gotten bitI've always like writing using the "or" form and have never =
=E2=80=9Cor=E2=80=9D introducted.=20
I, on the other hand, had to fix a production problem that using =
I'veI avoid this idiom because it fails on falsy values.=20
Perl has a // operator (pronounced "err"), which works like || (or),
except that it tests whether the left side is defined (not None in
Python terms) instead of truthy. This still isn't bulletproof but =
found it very handy.=20
=20
So, if starting from:
=20
def method( self, name=3DNone, ):
=20
rather than:
=20
self.name =3D name if name else default_value
=20
ie
=20
self.name =3D name if name is True else default_value
the more precise:
=20
self.name =3D name if name is not None or default_value
=20
or:
=20
self.name =3D default_value if name is None or name
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.
Loris Bennett wrote:
However, with a view to asking forgiveness rather than
permission, is there some simple way just to assign the dictionary
elements which do in fact exist to self-variables?
Assuming config is a dict:
self.__dict__.update( config )
Tobiah <toby@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 =3D 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 =3D 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
On Mon, 18 Mar 2024 10:09:27 +1300, dn wrote:'default'
YMMV!
NB your corporate Style Guide may prefer 'the happy path'...
If you only want to check for None, this works too:
'default'name = None
dafault_value = "default"
name or default_value
'Fred Flintstone'name = 'Fred Flintstone'
name or default_value
name = ''
name or default_value
'default'name = False
name or default_value
'default'name = []
name or default_value
'default'name = 0
name or default_value
You haven't only checked for None! You have rejected *every* falsish value, even though they may very well be acceptable values.
Sysop: | Tetrazocine |
---|---|
Location: | Melbourne, VIC, Australia |
Users: | 6 |
Nodes: | 8 (0 / 8) |
Uptime: | 47:41:24 |
Calls: | 45 |
Files: | 21,492 |
Messages: | 63,554 |