Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created January 14, 2019 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/156efa479172b6073f6cebb9cc7b92b4 to your computer and use it in GitHub Desktop.
Save Fhernd/156efa479172b6073f6cebb9cc7b92b4 to your computer and use it in GitHub Desktop.
Instanciar un objeto sin hacer uso del método __init__. Python.
class Fecha:
def __init__(self, aghnio, mes, dia):
self.aghnio = aghnio
self.mes = mes
self.dia = dia
def __str__(self):
return '{}-{}-{}'.format(self.aghnio, self.mes, self.dia)
if __name__ == '__main__':
fecha = Fecha.__new__(Fecha)
# Produce error. El atributo no ha sido definido aún:
# fecha.mes
setattr(fecha, 'aghnio', 2019)
setattr(fecha, 'mes', 1)
setattr(fecha, 'dia', 14)
print(fecha)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment