Conditions | 13 |
Total Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like test() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | from ne2001.utils import Class_Operation |
||
12 | def test(): |
||
13 | f1 = Foo(lambda x: 2*x, 1) |
||
14 | f2 = Foo(lambda x: x**2, 2) |
||
15 | |||
16 | f = f1 + f2 |
||
17 | assert f.x == f1.x + f2.x |
||
18 | assert f.func(3) == f1.func(3) + f2.func(3) |
||
19 | |||
20 | f = f1 - f2 |
||
21 | assert f.x == f1.x - f2.x |
||
22 | assert f.func(3) == f1.func(3) - f2.func(3) |
||
23 | |||
24 | f = f1 + f2 |
||
25 | assert f.x == f1.x + f2.x |
||
26 | assert f.func(3) == f1.func(3) + f2.func(3) |
||
27 | |||
28 | f = f1 * f2 |
||
29 | assert f.x == f1.x * f2.x |
||
30 | assert f.func(3) == f1.func(3) * f2.func(3) |
||
31 | |||
32 | f = max(f1, f2) |
||
33 | assert f.x == max(f1.x, f2.x) |
||
34 | assert f.func(3) == max(f1.func(3), f2.func(3)) |
||
35 |