Python example to define a class

Characterizing a class in Python: In this program/example, we will figure out how to characterize a class, how to characterize a trait, how to make an object of the class and how to get to the characteristic?

The undertaking to characterize a class in Python.

Here, we are characterizing a class named Number with a quality num, instating it with a worth 123, at that point making two items N1 and N2 lastly, printing the article’s memory areas and traits esteem utilizing the item names.

Python code to characterize a class:

# Python code to define a class

# class definition
class Number():
    #Attribute
    num = 123

# main code
if __name__ == "__main__": 
    # creating first object 
    N1 = Number()
    #Printing object's memory (in hexadecimal)
    print(N1)      
    #Accessing and printing Class's Attribute
    print(N1.num)  

    # creating first object 
    N2 = Number()
    #Printing object's memory (in hexadecimal)
    print(N2)      
    #Accessing and printing Class's Attribute
    print(N2.num)

Output:

<__main__.Number object at 0x7f96c06a0160>
123
<__main__.Number object at 0x7f96c06a06d8>
123

Leave a Comment

error: Alert: Content is protected!!