Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Function in class | Python | 4

Description:

In this post, I am going to show you how to define a class in python. How to define function in class and how to define variables in class.
So let's Start.

1) Define a function in class.

class DisplayMyName:                    //this is class
    def Name(self):                           // this is function in class
        pass                                //whenever we only define function and not body we use pass
obj=DisplayMyName()         // function call
obj.Name.__func__.data = 'my name is EasyCoding'               store string in data variable
print(obj.Name.data)                                      //print

Output:
my name is EasyCoding


2) Define variables in class. Sum two numbers.

class Sum:
a=22 b=12 passobj=Sum()
c=obj.a+obj.b
print(c)
output:
34


This post first appeared on AR Programming, please read the originial post: here

Share the post

Function in class | Python | 4

×

Subscribe to Ar Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×