4 ms·
After 5 years of Python, I don't even see self anymore (how zen). Author could have written class Window(object): def __init__(self, min, max):
by marmaduke 12y ago
After 5 years of Python, I don't even see self anymore (how zen).
Author could have written
class Window(object):
def __init__(self, min, max):
if min > max:
min, max = max, min
self.min = min
self.max = max
to avoid repetition. If many args are needed,
def __init__(self, a,b,c,d,e,f,...):
for k, v in locals().items():
setattr(self, k, v)
Etc etc there are endless idiomatic ways to avoid boilerplate in Python
- muhuk 12y agoOr this: class Window(object): def __init__(self, minimum, maximum): self.minimum = min(minimum, maximum) self.maximum = max(minimum, maximum) Why set attributes twice? AFAIAC you only need to write "self." once for each attribute in the constructor.
- droope 12y agoI agree, there is hardly any need for OPs post.
- MatthewWilkes 12y agoclass Window(object): def __init__(self, minimum, maximum): self.min_func = min self.max_func = max self.tuple_type = tuple self.minimum = minimum self.maximum = maximum self.minimum, self.maximum = self.tuple_type([self.min_func(*self.tuple_type([self.minimum, self.maximum])), self.max_func(*self.tuple_type([self.minimum, self.maximum]))]) def __call__(self, x): self.x = x result = self.minimum <= self.x <= self.maximum del self.x return result