How do i create an instance of an object in python?



  • In article https://www.lightnetics.com/post/9836 we showed how to use classes.

    This article shows how to create an instance of an object.

    class Cake(object):
    
        def __init__(self, type, base):
            self.type = type
            self.base = base
    
    
    c1 = Cake('carrot', 'sponge')
    print(c1.type)
    print(c1.base)
    
    c2 = Cake('fruit', 'sponge')
    print(c2.type)
    print(c2.base)
    

    c1 & c2 are instances and creating an instance using the object is shown Cake('fruit', 'sponge') & Cake('carrot', 'sponge')


Log in to reply
 

© Lightnetics 2024