I would like to make bool binary operations using the magic methods for these operators. For example, I can get a < b
as getattr(a, '__lt__')(b)
or a == b
as getattr(a, '__eq__')(b)
.
Can I get a in b
and a is b
in such a way?
I would like to make bool binary operations using the magic methods for these operators. For example, I can get a < b
as getattr(a, '__lt__')(b)
or a == b
as getattr(a, '__eq__')(b)
.
Can I get a in b
and a is b
in such a way?
in
is __contains__
and is
does not have a dunder method. I strongly suggest you use the functions in the operator
module:
a < b => operator.lt(a, b)
a == b => operator.eq(a, b)
a in b => operator.contains(a, b)
a is b => operator.is_(a, b)
getattr(a, '__lt__')(b)
as asking for the function version of a < b
which is operator.lt(a, b)
. You can pass around operator.lt
. I saw no attempt at definition of a class in which you would define the dunder methods. –
Phira getattr
. –
Salzhauer operator.is_
with the first argument already filled in, like getattr(a, '__lt__')
already has a
as its first argument) you can use functools.partial
. partial(operator.is_, a)
is a function that only takes one argument. –
Morphosis For in
, the correct dunder method is __contains__
.
There is no method for is
, because this is equivalent to id(a) == id(b)
. It compares the actual object ID used under the hood by Python, so is used to compare object identity, not object contents. Overwriting it within a class would break Python's object model, so it is not allowed.
in
is __contains__
and is
does not have a dunder method. I strongly suggest you use the functions in the operator
module:
a < b => operator.lt(a, b)
a == b => operator.eq(a, b)
a in b => operator.contains(a, b)
a is b => operator.is_(a, b)
operator
module be preferable to the usual way? –
Rhatany getattr(a, '__lt__')(b)
as asking for the function version of a < b
which is operator.lt(a, b)
. You can pass around operator.lt
. I saw no attempt at definition of a class in which you would define the dunder methods. –
Phira getattr
. –
Salzhauer operator.is_
with the first argument already filled in, like getattr(a, '__lt__')
already has a
as its first argument) you can use functools.partial
. partial(operator.is_, a)
is a function that only takes one argument. –
Morphosis __contains__
is correct for in
, with fall-back options if __contains__
isn't defined being __iter__
and __getitem__
.
I am not really sure why you'd need to use getattr
for is
though; is
is "defined" for every object in Python. There's no need to go through operator._is
or (trying and failing) through getattr
.
See the documentation on built-in types:
The behavior of the
is
andis not
operators cannot be customized; also they can be applied to any two objects and never raise an exception.
(Emphasis mine)
According to the snippets you provided, which just grab a function and call it using getattr(a, "function")(b)
, you already have the names of the objects you need to evaluate, just use is
immediately; it is always available.
© 2022 - 2024 — McMap. All rights reserved.
operator
module be preferable to the usual way? – Rhatany