Python - Herencia en __init__ python

 
Vista:

Herencia en __init__ python

Publicado por Ronaldo Rodríguez (1 intervención) el 11/03/2023 23:10:01
Hola amigos, cómo puedo pasar este ultimo test en principio lo estoy haciendo bien pero sigue sin pasar
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
101
102
103
104
105
106
107
108
109
110
111
import dis
import os
 
 
class Dessert:
 
 
    def __init__(self, name, calories):
        self.name = name
        self.calories = calories
    def healthy(self):
        if self.calories < 200:
            healthy = True
        else:
            healthy = False
        return healthy
 
 
    def delicious(self):
        return True
 
 
class JellyBean(Dessert):
 
    def __init__(self, name, calories,flavor):
        super().__init__(name, calories)
        self.flavor=flavor
 
    def delicious(self):
        if self.flavor=='black licorice':
            return False
        else:
            return super().delicious()
 
 
 
print("##########################################")
print("##################TESTS###################")
print("##########################################\n")
print('Testing your Dessert methods')
healthy_des = Dessert('cupcake', 100)
unhealthy_des = Dessert('cake', 300)
if healthy_des.healthy():
    os.system("echo '\e[32mCorrect, healthy if under 200 calories\e[0m'")
else:
    os.system("echo '\e[31mIncorrect, should be healthy if under 200 calories\e[0m'")
if not unhealthy_des.healthy():
    os.system("echo '\e[32mCorrect, not healthy if over 200 calories\e[0m'")
else:
    os.system("echo '\e[31mIncorrect, should not be healthy if over 200 calories\e[0m'")
if healthy_des.delicious():
    os.system("echo '\e[32mCorrect, Dessert is always delicious\e[0m'")
else:
    os.system("echo '\e[31mIncorrect, Dessert is always delicious\e[0m'")
if unhealthy_des.delicious():
    os.system("echo '\e[32mCorrect, Dessert is always delicious\e[0m'")
else:
    os.system("echo '\e[31mIncorrect, Dessert is always delicious\e[0m'")
print('Testing your JellyBean methods')
delicious_des = JellyBean('yummy', 100, 'cherry')
gross_des = JellyBean('disgusting', 300, 'black licorice')
if delicious_des.healthy():
    os.system("echo '\e[32mCorrect, healthy if under 200 calories\e[0m'")
else:
    os.system("echo '\e[31mIncorrect, should be healthy if under 200 calories\e[0m'")
if not gross_des.healthy():
    os.system("echo '\e[32mCorrect, not healthy if over 200 calories\e[0m'")
else:
    os.system("echo '\e[31mIncorrect, should not be healthy if under 200 calories\e[0m'")
if delicious_des.delicious():
    os.system("echo '\e[32mCorrect, most flavors are delicious\e[0m'")
else:
    os.system("echo '\e[31mIncorrect, only black licorice is not delicious\e[0m'")
if not gross_des.delicious():
    os.system("echo '\e[32mCorrect, black licorice is not delicious\e[0m'")
else:
    os.system("echo '\e[31mIncorrect, black licorice is not delicious\e[0m'")
print('Testing your JellyBean class Inheritance')
 
 
def list_method_calls(fn):
 
 
    methods = []
    bytecode = dis.Bytecode(fn)
    instrs = list(reversed([instr for instr in bytecode]))
    for ix, instr in enumerate(instrs):
        if instr.opname == "CALL_FUNCTION":
            load_method_instr = instrs[ix + instr.arg + 1]
            methods.append(load_method_instr.argval)
    return methods
 
init_calls = list_method_calls(JellyBean.__init__)
delicious_calls = list_method_calls(JellyBean.delicious)
print('Rons',delicious_calls)
if len(JellyBean.__mro__) == 3:
    os.system("echo '\e[32mCorrect, JellyBean has a parent class\e[0m'")
else:
    os.system("echo '\e[31mIncorrect, JellyBean does not inherit from a parent class\e[0m'")
if 'healthy' not in JellyBean.__dict__.keys():
    os.system("echo '\e[32mCorrect, JellyBean inherits the healthy method\e[0m'")
else:
    os.system("echo '\e[31mIncorrect, JellyBean should inherit the healthy method\e[0m'")
if 'super' in init_calls:
    os.system("echo '\e[32mCorrect, JellyBeans __init__ method is DRY the healthy method\e[0m'")
else:
    os.system("echo '\e[31mIncorrect, JellyBeans __init__ method should inherit from Dessert\e[0m'")
if 'super' in delicious_calls:
    os.system("echo '\e[32mCorrect, JellyBeans delicious method inherits from Dessert\e[0m'")
else:
    os.system("echo '\e[31mIncorrect, JellyBeans delicious method should inherit from Dessert\e[0m'")



Tests :

Testing your Dessert methods
'\e[32mCorrect, healthy if under 200 calories\e[0m'
'\e[32mCorrect, not healthy if over 200 calories\e[0m'
'\e[32mCorrect, Dessert is always delicious\e[0m'
'\e[32mCorrect, Dessert is always delicious\e[0m'
Testing your JellyBean methods
'\e[32mCorrect, healthy if under 200 calories\e[0m'
'\e[32mCorrect, not healthy if over 200 calories\e[0m'
'\e[32mCorrect, most flavors are delicious\e[0m'
'\e[32mCorrect, black licorice is not delicious\e[0m'
Testing your JellyBean class Inheritance
Rons []
'\e[32mCorrect, JellyBean has a parent class\e[0m'
'\e[32mCorrect, JellyBean inherits the healthy method\e[0m'
'\e[31mIncorrect, JellyBeans __init__ method should inherit from Dessert\e[0m'
'\e[31mIncorrect, JellyBeans delicious method should inherit from Dessert\e[0m'
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Herencia en __init__ python

Publicado por Ronaldo Rodríguez (1 intervención) el 15/03/2023 04:45:39
ChatGPT(?)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar