Velocity Reviews > epiphany

# epiphany

Roy Smith
Guest
Posts: n/a

 04-24-2013
I discovered something really neat today.

We've got a system with a bunch of rules. Each rule is a method which
returns True or False. At some point, we need to know if all the rules
are True. Complicating things, not all the rules are implemented.
Those that are not implemented raise NotImplementedError.

We used to have some ugly logic which kept track of which rules were
active and only evaluated those.

So, here's the neat thing. It turns out that bool(NotImplemented)
returns True. By changing the unimplemented rules from raising
NotImplementedError to returning NotImplemented, the whole thing becomes:

return all(r() for r in rules)

Steven D'Aprano
Guest
Posts: n/a

 04-25-2013
On Wed, 24 Apr 2013 19:50:33 -0400, Roy Smith wrote:

> I discovered something really neat today.
>
> We've got a system with a bunch of rules. Each rule is a method which
> returns True or False. At some point, we need to know if all the rules
> are True. Complicating things, not all the rules are implemented. Those
> that are not implemented raise NotImplementedError.

NotImplementedError is intended to be raised by abstract base classes to
indicate a method that must be overridden. I also use it as a place-
holder for functions or methods I haven't actually written yet. I'm not
sure what semantics you're giving NotImplementedError in your code, but I
wonder whether a neater solution might be to just use rule = None for
unimplemented rules, rather than:

def unimplemented():
raise NotImplementedError

rule = unimplemented

Then your logic for seeing if all rules return true would become:

all(r() for r in rules if r is not None)

and for seeing if all rules return true or are unimplemented:

all(r is None or r() for r in rules)

> We used to have some ugly logic which kept track of which rules were
> active and only evaluated those.

I don't see why you would need anything like that. Reading further on, I
see that you are counting unimplemented rules as true, for some reason
which I don't understand. (Knowing nothing of your use-case, I would have
expected intuitively that unimplemented rules count as not true.) A
simple helper function will do the job:

def eval(rule):
try:
return rule()
except NotImplementedError:
return True

everything_is_true = all(eval(r) for r in rules)

No need for complicated ugly logic keeping track of what rules are
implemented. But if you're worried about the cost of catching those
exceptions (you've profiled your code, right?) then that's easy with a
decorator:

def not_implemented(func):
@functools.wraps(func)
def inner(*args, **kw):
raise NotImplementedError
inner.ni = True
return inner

# Decorate only the rules you want to be unimplemented.

@not_implemented
def my_rule():
pass

everything_is_true = all(r() for r in rules if not hasattr(r, 'ni'))

Note that if you could reverse the logic so that unimplemented rules
count as not true, this will also work:

try:
everything_is_true = all(r() for r in rules)
except NotImplementedError:
everything_is_true = False

> So, here's the neat thing. It turns out that bool(NotImplemented)
> returns True. By changing the unimplemented rules from raising
> NotImplementedError to returning NotImplemented, the whole thing
> becomes:
>
> return all(r() for r in rules)

Objects are supposed to return NotImplemented from special dunder methods
like __add__, __lt__, etc. to say "I don't know how to implement this
method for the given argument". Python will then try calling the other
object's special method. If both objects return NotImplemented, Python
falls back on whatever default behaviour is appropriate.

So, knowing nothing of your application, I fear that this is an abuse of
NotImplemented's semantics. If a rule returns NotImplemented, I would
expect your application to fall back on a different rule. If that's not
the case, you're using it in a non-standard way that will cause confusion
for those with expectations of what NotImplemented means.

--
Steven

Roy Smith
Guest
Posts: n/a

 04-25-2013
In article <5178884b\$0\$29977\$c3e8da3\$(E-Mail Removed) om>,
Steven D'Aprano <(E-Mail Removed)> wrote:

> I don't see why you would need anything like that. Reading further on, I
> see that you are counting unimplemented rules as true, for some reason
> which I don't understand.

The top-level logic we need to enforce is "this configuration doesn't
violate any rules".

Chris Angelico
Guest
Posts: n/a

 04-25-2013
On Thu, Apr 25, 2013 at 11:41 AM, Roy Smith <(E-Mail Removed)> wrote:
> In article <5178884b\$0\$29977\$c3e8da3\$(E-Mail Removed) om>,
> Steven D'Aprano <(E-Mail Removed)> wrote:
>
>> I don't see why you would need anything like that. Reading further on, I
>> see that you are counting unimplemented rules as true, for some reason
>> which I don't understand.

>
> The top-level logic we need to enforce is "this configuration doesn't
> violate any rules".

Then have your unimplemented rules simply return True. Easy!

ChrisA

Ethan Furman
Guest
Posts: n/a

 04-25-2013
On 04/24/2013 06:35 PM, Steven D'Aprano wrote:
> Objects are supposed to return NotImplemented from special dunder methods
> like __add__, __lt__, etc. to say "I don't know how to implement this
> method for the given argument". Python will then try calling the other
> object's special method. If both objects return NotImplemented, Python
> falls back on whatever default behaviour is appropriate.
>
> So, knowing nothing of your application, I fear that this is an abuse of
> NotImplemented's semantics. If a rule returns NotImplemented, I would
> expect your application to fall back on a different rule. If that's not
> the case, you're using it in a non-standard way that will cause confusion
> for those with expectations of what NotImplemented means.

Why would you assume some random application is going to deal with NotImplemented the same way the python interpreter
does? And even the interpreter isn't consistent -- sometimes it will return false (__eq__) and sometimes it will raise

I hardly think it an abuse of NotImplemented to signal something is not implemented when NotImplemented means, um, not
implemented.

possibly-not-implemented-ly yours,

--
~Ethan~

Ethan Furman
Guest
Posts: n/a

 04-25-2013
On 04/24/2013 07:20 PM, Chris Angelico wrote:
> On Thu, Apr 25, 2013 at 11:41 AM, Roy Smith <(E-Mail Removed)> wrote:
>> In article <5178884b\$0\$29977\$c3e8da3\$(E-Mail Removed) om>,
>> Steven D'Aprano <(E-Mail Removed)> wrote:
>>
>>> I don't see why you would need anything like that. Reading further on, I
>>> see that you are counting unimplemented rules as true, for some reason
>>> which I don't understand.

>>
>> The top-level logic we need to enforce is "this configuration doesn't
>> violate any rules".

>
> Then have your unimplemented rules simply return True. Easy!

And less clear.

--
~Ethan~

Roy Smith
Guest
Posts: n/a

 04-25-2013
In article <(E-Mail Removed)>,
Chris Angelico <(E-Mail Removed)> wrote:

> On Thu, Apr 25, 2013 at 11:41 AM, Roy Smith <(E-Mail Removed)> wrote:
> > In article <5178884b\$0\$29977\$c3e8da3\$(E-Mail Removed) om>,
> > Steven D'Aprano <(E-Mail Removed)> wrote:
> >
> >> I don't see why you would need anything like that. Reading further on, I
> >> see that you are counting unimplemented rules as true, for some reason
> >> which I don't understand.

> >
> > The top-level logic we need to enforce is "this configuration doesn't
> > violate any rules".

>
> Then have your unimplemented rules simply return True. Easy!
>
> ChrisA

It's nice to have tri-state logic:

* This rule passes

* This rule fails

* This rule was not evaluated

What I've got now expresses that perfectly.

Steven D'Aprano
Guest
Posts: n/a

 04-25-2013
On Wed, 24 Apr 2013 19:25:37 -0700, Ethan Furman wrote:

> On 04/24/2013 06:35 PM, Steven D'Aprano wrote:
>> Objects are supposed to return NotImplemented from special dunder
>> methods like __add__, __lt__, etc. to say "I don't know how to
>> implement this method for the given argument". Python will then try
>> calling the other object's special method. If both objects return
>> NotImplemented, Python falls back on whatever default behaviour is
>> appropriate.
>>
>> So, knowing nothing of your application, I fear that this is an abuse
>> of NotImplemented's semantics. If a rule returns NotImplemented, I
>> would expect your application to fall back on a different rule. If
>> that's not the case, you're using it in a non-standard way that will
>> cause confusion for those with expectations of what NotImplemented
>> means.

>
> Why would you assume some random application is going to deal with
> NotImplemented the same way the python interpreter does?

Why would you assume that some random application is going to treat x==y
the same way the Python interpreter does?

Just because you can design your objects to do anything you want doesn't
mean you should. Breaking conventions carries costs by the mere fact that
you're breaking conventions. There are established semantics that an
experienced Python developer will expect for NotImplemented, and doing
something else risks causing confusion and mistakes.

Or worse, bugs. If there is any chance that a rule might be called in a
context where the Python interpreter gets to interpret the return result
before you see it, then returning NotImplemented could lead to difficult
to debug problems.

> And even the
> interpreter isn't consistent -- sometimes it will return false (__eq__)
> and sometimes it will raise an Exception (__add__).

As I said:

"If both objects return NotImplemented, Python falls back on whatever
default behaviour is appropriate."

If neither object knows how to compare the other for equality, the
appropriate behaviour is to treat them as unequal. If neither object
knows how to add itself to the other, the appropriate behaviour is to
raise an exception.

> I hardly think it an abuse of NotImplemented to signal something is not
> implemented when NotImplemented means, um, not implemented.

It doesn't just mean "not implemented in general", it has a specific
meaning: "I don't know what to do here, let the other object handle it".

As I have repeatedly said, I don't know the context of the application,
but from what little has been described, this part of it doesn't feel to
me like a good, clean design. I might be wrong, but from the outside it
feels like the API should be that rules return a three-state logic
instance:

True, False, Unknown

where Unknown can be trivially created with

Unknown = object()

The semantics of NotImplementedError is that it is an *error*, and that
doesn't sound appropriate given the example shown. Why would a rule that
raises an *error* exception be treated as if it had passed? That's just
wrong.

The semantics of NotImplemented is that it is a signal for one object to
say "I don't know how to do this, let somebody else try". That also
doesn't seem appropriate. There's no sign that Roy's application does the
equivalent to this:

result = rule()
if result is NotImplemented:
result = another_rule()
if result is NotImplemented:
result = some_default

Since rules apparently take no arguments, either:

1) they rely on global state, which is a nasty design; or

2) rules actually have a fixed return result, in which case why make them
functions in the first place?

Since both possibilities seem stupid, and I do not believe that Roy
actually is stupid, I suspect that his example over-simplifies the
situation. But I can't comment on the infinite number of things that his
code might do, I can only comment on the examples as actually given, and
as given, I don't think that either NotImplementedError or NotImplemented
is a clean solution to the problem.

--
Steven

Roy Smith
Guest
Posts: n/a

 04-25-2013
In article <5178b1db\$0\$29977\$c3e8da3\$(E-Mail Removed) om>,
Steven D'Aprano <(E-Mail Removed)> wrote:

> The semantics of NotImplemented is that it is a signal for one object to
> say "I don't know how to do this, let somebody else try".

That's precisely the logic here. The rule says, "I don't know how to
tell you if this is OK or not, ask another rule".

> Since rules apparently take no arguments, either:
>
> 1) they rely on global state, which is a nasty design; or
>
> 2) rules actually have a fixed return result, in which case why make them
> functions in the first place?

Yes, rules take arguments. I elided them from the original description
since it wasn't germane to what I was trying to show.

> Since both possibilities seem stupid, and I do not believe that Roy
> actually is stupid,

I am honored that you have such a high opinion of me

Here's what the docs say about NotImplemented:

> This type has a single value. There is a single object with this
> value. This object is accessed through the built-in name
> NotImplemented. Numeric methods and rich comparison methods may
> return this value if they do not implement the operation for the
> operands provided. (The interpreter will then try the reflected
> operation, or some other fallback, depending on the operator.) Its
> truth value is true.

It gives an example of a use by numeric methods. It doesn't say that's
the only thing it can be used for.

It also says, "Its truth value is true". Why would they document that
fact if you weren't supposed to use it as a boolean operand?

Steven D'Aprano
Guest
Posts: n/a

 04-25-2013
On Thu, 25 Apr 2013 08:36:34 -0400, Roy Smith wrote:

> In article <5178b1db\$0\$29977\$c3e8da3\$(E-Mail Removed) om>,
> Steven D'Aprano <(E-Mail Removed)> wrote:
>
>> The semantics of NotImplemented is that it is a signal for one object
>> to say "I don't know how to do this, let somebody else try".

>
> That's precisely the logic here. The rule says, "I don't know how to
> tell you if this is OK or not, ask another rule".

Sounds good to me then. It looks like your design is actually much closer
to what I believe the standard Python semantics are intended to be than
it appeared at first.

[...]
> Here's what the docs say about NotImplemented:
>
>> This type has a single value. There is a single object with this value.
>> This object is accessed through the built-in name NotImplemented.
>> Numeric methods and rich comparison methods may return this value if
>> they do not implement the operation for the operands provided. (The
>> interpreter will then try the reflected operation, or some other
>> fallback, depending on the operator.) Its truth value is true.

>
> It gives an example of a use by numeric methods. It doesn't say that's
> the only thing it can be used for.

Right. You can do a lot of things in Python, including shooting your foot
off but that doesn't mean you should. The further away from standard
Python conventions you get, the more wary you should be. That's all.

> It also says, "Its truth value is true". Why would they document that
> fact if you weren't supposed to use it as a boolean operand?

You can use *anything* in Python in a boolean context. That's a language
feature: all objects are either truthy or falsey. As for why it is
documented for NotImplemented, I guess that's because some people might
guess that it is falsey, like None.

--
Steven