operator overloading - Overload Python 'in' to return non-bool -
I overload the in
operator to return a non-bowl object to a code Trying to do it, but it seems to be doing anyway. Here is my use case:
class dataset (object): def __init __ (self): self._filters = [] DF filter (self, f): self._filters.append (f ) Returns Self Class Equal Filter (def): def __init __ (Self, Field, Val): ... Class Subfilter (object): def __init __ (self, field, wall): ... Class filter builder (object): def __init __ (self, area): self._field = area def __eq __ (self, val): return similar filter (self._field, wal) def __contains __ (auto, vals): return SubsetFilter (self._field, vals) veggie = FilterBuilder ('v The fruit = 'filter' ('fruit') DS = dataset (). Filter (veggie == 'carrot') (fruit in '[apple', 'orange']) ). At the end of the code,
, andDS
in thesimilar filter
for veggie == 'carrot'true
Forin fruit ['apple', 'orange']
.subset for DS
Here the real problem is that, as it says:
For defined classes, which defines
___ ()
method,x in y
is true if and only ify__ in __ (x) < / Code> is true. See also
.
The answer to user2357112 can be that it is better than me. The list contains .___ __
, not is included in the FilterBuilder .__
.
But why does this work?
Well, how else can this work?
Imagine that in [code> 3 [1, 2, 3] is included in int____ __
. And 3 in {1, 2, 3}
and 3 my_custom_sorted_bintree
How can it be included in the int .__ (Container)
Can possibly be implemented? Definitely not to run on the container again, it means slow, detailed search to see things in sets and bintries, which will defeat the whole point. Maybe my Bantry class may not be weak, and even then there is a concept of membership.
But if they call list then include .___ __
, set .__ contains____
and
are included in CustomSortedBintree .__ __
? Do not they need to know about int, and str, and can you possibly give them all possible? No, not at all. A list should know how to compare arg == elem
for each element of its one set should also know how to call hash
A bintry should also know how to call arg & lt; ELEM
. But you do not need to know anything about the type of arg
to do this.
You probably want to know how to handle it. Here are two and a half simple solutions.
1: You can easily create a FilterList
class. Then, you just write:
Fruit ('apple', 'orange') in the filialist
1.5: Or, with a little more work, 2] Alternatively, you can create a more general "value holder":
In the fruit const (['apple', 'orange']) a FilterBuilder.in _
method. Then you write: fruit.in _ (['apple', 'orange'])
... or, if you wish:
Most libraries I have seen either provide second (skylchamy), or both, but use the other in their tutorials (though in Episcripts), though " Quick-Lambda "libraries often go with the earlier generic version.
But you should consider trading ideas in terms of your use. Generally, it is easier to apply first, is more clear, and its advantage is that subfilters / subqueries can return something which functions as FilterList
; The other thing is less and it is arguably easy to read.
If no one is acceptable, you might consider writing a Python, such as using an expression template or something like using DSL for a Python instead of trying to create DSL with the actual Python code. Doing (which I think is the type of search you need, along with the need of instant-lambda macros which do not require "CONST" and friends)
Comments
Post a Comment