# Una clase base genérica proporciona getters para todos los atributos
# de las clases derivadas.
class Generica:
Prefijo_Atributos = "__"
def __getattr__(self, item_name):
if item_name.startswith("get_"):
attr_name = Generica.Prefijo_Atributos + item_name[len(Generica.Prefijo_Atributos):].lower()
exists = True
try:
attr = self.__dict__[attr_name]
except KeyError:
exists = False
if (exists
and not callable(attr)):
return lambda: attr
class Persona(Generica):
def __init__(self, nombre, email, edad):
self.__dict__[Generica.Prefijo_Atributos + "nombre"] = nombre
self.__dict__[Generica.Prefijo_Atributos + "email"] = email
self.__dict__[Generica.Prefijo_Atributos + "edad"] = edad
def __str__(self):
return (self.get_nombre() + " ("
+ str(self.get_edad()) + "): " + self.get_email())
if __name__ == "__main__":
p = Persona("Baltasar", "jbgarcia@uvigo.es", 18)
print("Nombre:", p.get_nombre())
print("Email:", p.get_email())
print("Edad:", p.get_edad())
print(p)
IyBVbmEgY2xhc2UgYmFzZSBnZW7DqXJpY2EgcHJvcG9yY2lvbmEgZ2V0dGVycyBwYXJhIHRvZG9zIGxvcyBhdHJpYnV0b3MKIyBkZSBsYXMgY2xhc2VzIGRlcml2YWRhcy4KCgpjbGFzcyBHZW5lcmljYToKICAgIFByZWZpam9fQXRyaWJ1dG9zID0gIl9fIgogICAgCiAgICBkZWYgX19nZXRhdHRyX18oc2VsZiwgaXRlbV9uYW1lKToKICAgICAgICBpZiBpdGVtX25hbWUuc3RhcnRzd2l0aCgiZ2V0XyIpOgogICAgICAgICAgICBhdHRyX25hbWUgPSBHZW5lcmljYS5QcmVmaWpvX0F0cmlidXRvcyArIGl0ZW1fbmFtZVtsZW4oR2VuZXJpY2EuUHJlZmlqb19BdHJpYnV0b3MpOl0ubG93ZXIoKQogICAgICAgICAgICBleGlzdHMgPSBUcnVlCiAgICAgICAgICAgIAogICAgICAgICAgICB0cnk6CiAgICAgICAgICAgICAgICBhdHRyID0gc2VsZi5fX2RpY3RfX1thdHRyX25hbWVdCiAgICAgICAgICAgIGV4Y2VwdCBLZXlFcnJvcjoKICAgICAgICAgICAgICAgIGV4aXN0cyA9IEZhbHNlCgogICAgICAgICAgICBpZiAoZXhpc3RzCiAgICAgICAgICAgIGFuZCBub3QgY2FsbGFibGUoYXR0cikpOgogICAgICAgICAgICAgICAgcmV0dXJuIGxhbWJkYTogYXR0cgoKCmNsYXNzIFBlcnNvbmEoR2VuZXJpY2EpOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIG5vbWJyZSwgZW1haWwsIGVkYWQpOgogICAgICAgIHNlbGYuX19kaWN0X19bR2VuZXJpY2EuUHJlZmlqb19BdHJpYnV0b3MgKyAibm9tYnJlIl0gPSBub21icmUKICAgICAgICBzZWxmLl9fZGljdF9fW0dlbmVyaWNhLlByZWZpam9fQXRyaWJ1dG9zICsgImVtYWlsIl0gPSBlbWFpbAogICAgICAgIHNlbGYuX19kaWN0X19bR2VuZXJpY2EuUHJlZmlqb19BdHJpYnV0b3MgKyAiZWRhZCJdID0gZWRhZAogICAgICAgIAogICAgZGVmIF9fc3RyX18oc2VsZik6CiAgICAgICAgcmV0dXJuIChzZWxmLmdldF9ub21icmUoKSArICIgKCIKICAgICAgICAgICAgICAgICsgc3RyKHNlbGYuZ2V0X2VkYWQoKSkgKyAiKTogIiArIHNlbGYuZ2V0X2VtYWlsKCkpCiAgCiAgCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6CiAgICBwID0gUGVyc29uYSgiQmFsdGFzYXIiLCAiamJnYXJjaWFAdXZpZ28uZXMiLCAxOCkKICAgIHByaW50KCJOb21icmU6IiwgcC5nZXRfbm9tYnJlKCkpCiAgICBwcmludCgiRW1haWw6IiwgcC5nZXRfZW1haWwoKSkKICAgIHByaW50KCJFZGFkOiIsIHAuZ2V0X2VkYWQoKSkKICAgIHByaW50KHApCg==