Constructor y Destructor en Python

#!/usr/bin/env python # -*- coding: utf-8 -*- class Tipo: atributo = '' valor = 0 def __init__(self): print("Objeto Tipo creado e inicializado") def __del__(self): print("Objeto Tipo destruido") def __str__(self): return "Tipo{ atributo: "+self.atributo+", valor: "+str(self.valor)+" }" def __repr__(self): return {'atributo':self.atributo, 'valor':str(self.valor)} def main(): tipo = Tipo() tipo.atributo = "Atributo" tipo.valor = 90 print(tipo) #Destruimos objeto 'tipo' del tipo if __name__ == '__main__': main()
Ejemplo de constructor y destructor en Python

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.