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
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