Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
| Ambdós costats versió prèvia Revisió prèvia Següent revisió | Revisió prèvia | ||
| info:cursos:pue:python-pcpp1:m1:5.1 [05/11/2023 21:35] – ↷ Page moved from info:cursos:pue:python-pcpp1:5.1 to info:cursos:pue:python-pcpp1:m1:5.1 mate | info:cursos:pue:python-pcpp1:m1:5.1 [06/11/2023 10:03] (actual) – [resposta] mate | ||
|---|---|---|---|
| Línia 329: | Línia 329: | ||
| def get_instantiation_time(self): | def get_instantiation_time(self): | ||
| human_readable = datetime.datetime.fromtimestamp(self.instantiation_time) | human_readable = datetime.datetime.fromtimestamp(self.instantiation_time) | ||
| - | print(f" | + | print(f" |
| class MetaClassInstantation(type): | class MetaClassInstantation(type): | ||
| Línia 336: | Línia 336: | ||
| if ' | if ' | ||
| dictionary[' | dictionary[' | ||
| - | MetaClassInstantation.instantiated.append(MetaClassInstantation.__name__) | + | MetaClassInstantation.instantiated.append(name) |
| obj = super().__new__(mcs, | obj = super().__new__(mcs, | ||
| obj.instantiation_time = time.time() | obj.instantiation_time = time.time() | ||
| Línia 353: | Línia 353: | ||
| dos.get_instantiation_time() | dos.get_instantiation_time() | ||
| + | print(MetaClassInstantation.instantiated) | ||
| </ | </ | ||
| <code ; output> | <code ; output> | ||
| - | Hora d' | + | Hora d' |
| - | Hora d' | + | Hora d' |
| + | [' | ||
| + | </ | ||
| + | |||
| + | <code python ; resposta oficial> | ||
| + | import time | ||
| + | |||
| + | def get_class_instantiation_time(self): | ||
| + | return self.class_instantiation_time | ||
| + | |||
| + | class CleanCodeGuard(type): | ||
| + | classes_created = [] | ||
| + | |||
| + | def __new__(mcs, | ||
| + | if ' | ||
| + | dictionary[' | ||
| + | obj = super().__new__(mcs, | ||
| + | |||
| + | obj.class_instantiation_time = time.time() | ||
| + | CleanCodeGuard.classes_created.append(name) | ||
| + | time.sleep(1) | ||
| + | return obj | ||
| + | |||
| + | class My_Class1(metaclass=CleanCodeGuard): | ||
| + | pass | ||
| + | |||
| + | class My_Class2(metaclass=CleanCodeGuard): | ||
| + | pass | ||
| + | |||
| + | my_object1 = My_Class1() | ||
| + | print(my_object1.get_class_instantiation_time()) | ||
| + | |||
| + | my_object2 = My_Class2() | ||
| + | print(my_object2.get_class_instantiation_time()) | ||
| + | |||
| + | print(CleanCodeGuard.classes_created) | ||
| </ | </ | ||