¿Qué tienen en común los candidatos presidenciales y el lenguaje Python? Mucho más de lo que usted cree

Al menos Python es más fácil de entender :-). También me dió un excusa para mostrarles un poco de herencia y otros trucos de objetos en el lenguaje:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
# A little fun with the candidates for the US presidency for 2016 election year
# @author josevnz@kodegeek.com
#
class Candidate:
 
    def __init__(self, name, party, sex="F", age=18):
        self.__name = name
        self.__party = party
        self.__sex = "F" if sex not in ["M", "F"] else sex
        self.__age = 18 if not (18 < = age <= 120) else age
 
    @property
    def name(self):
        return self.__name
 
    @property
    def party(self):
        return self.__party
 
    @property
    def sex(self):
        return self.__sex
 
    @property
    def age(self):
        return self.__age
 
    def __hash__(self):
        return hash(id(self))
 
    def __str__(self):
        return "Candidate=[name={0}, party={1}, sex={2}, age={3}]".format(
                                                                      self.__name,
                                                                      self.__party,
                                                                      self.__sex,
                                                                      self.__age
                                                                      )
 
    def __repr__(self):
        return "{0}{1}{2}{3}{4}".format(
                                        self.__class__.__name__, 
                                        self.__name, 
                                        self.__party, 
                                        self.__sex, 
                                        self.__age
                                        )
 
    def __invert__(self):
        raise NotImplemented()
 
    # Are you a Democrat?    
    def __bool__(self):
        raise NotImplemented()
 
    def __lt__(self, other):
        return self.__age < other.__age
 
    def __gt__(self, other):
        return self.__age > other.__age
 
    def __eq__(self, other):
        return (
                self.__age == other.__age and 
                self.__name == other.__name and 
                self.__party == other.__party and 
                self.__sex == other.__sex
                )
 
class Republican(Candidate):
 
    def __init__(self, name, sex="F", age=21):
        return super().__init__(name, "Republican party", sex, age)
 
    def __bool__(self):
        return False
 
    def __invert__(self):
        raise NotImplementedError()
 
class Democrat(Candidate):
 
    def __init__(self, name, sex="F", age=21):
        return super().__init__(name, "Democratic party", sex, age)
 
    def __bool__(self):
        return True
 
    def __invert__(self):
        raise NotImplementedError()
 
if __name__ == "__main__":
        candidates = []
        candidates.append(Democrat("Hillary Clinton", "F", 68))
        candidates.append(Republican("Donald Trump", "M", 70))
        candidates.append(Democrat("Bernie Sanders", "M", 75))
        candidates.append(Republican("Marco Rubio", "M", 45))
        for candidate in sorted(candidates, reverse=True):
            isDemocrat = "yes" if candidate else "no"
            print("Candidate: {0}, democrat? {1}".format(candidate, isDemocrat))

And the output for this run:

1
2
3
4
Candidate: Candidate=[name=Bernie Sanders, party=Democratic party, sex=M, age=75], democrat? yes
Candidate: Candidate=[name=Donald Trump, party=Republican party, sex=M, age=70], democrat? no
Candidate: Candidate=[name=Hillary Clinton, party=Democratic party, sex=F, age=68], democrat? yes
Candidate: Candidate=[name=Marco Rubio, party=Republican party, sex=M, age=45], democrat? no

Ahora si sólo pudiera escribir algo tan sencillo como esto para saber los resultados de las elecciones del 2016 🙂