Movatterモバイル変換
[0]ホーム
[Python-Dev] Re: PEP 285 (Adding a bool type) extending it further
Marcelo Matusmmatus@dinha.acms.arizona.edu
Thu, 03 Oct 2002 17:25:01 -0700
To solve the problem of short-circuiting 'and' and 'or',the BooleanMethod class could be tested for nonzero method components, ie, you befinethe methods to null, likePyBooleanMethods { binaryfunc bl_and; binaryfunc bl_or; binaryfunc bl_not;} PyBooleanMethods;static PyBooleanMethods default_as_boolean = { 0, /* bl_and */ 0, /* bl_or */ 0, /* bl_not */};and overwrite them only when the __land__, __lor__or __lnot__ are defined.Then, when you perfom the 'add' or 'or' operations, youcheck for the proper boolean method in the firstargument only, which anyway have to be evaluated.If the boolean method exists, then youforce the evaluation of the second argumentand the apply the proper method. If not, youdo the usual, which will be the traditional short-circuitingoperation, ie: a and bwill be equivalent to if a.bl_and exist: return a.bl_and(b) else return a && b (traditional form with short-circuiting)and similar for 'or'.In this way, the implementation will be fully compatible with allthe previous code, allowing short-circuiting for the classesthat doesn't define the boolean methods (all of them by now),but will allow you to extend the operation for classes withtheir own version of the logical operations.Of course this requires that if you want to use your ownlogical operations, both or at least the firstargument in the operation must always be one of your specialclasses. But with proper documentations and warnings about it,I guess this alternative is better than nothing.Marcelo
[8]ページ先頭