method in class in python by R4R Team

Methods-
-A special kind of function that is defined in a class definition.
-In object orientation programming, function is said to be a method.

Syntax for calling a method-

object.method()


program-

#create class
class Computer():
i=10
s="This is class"
def hello(self):
print("hello")
def hii(self):
print("Hii")
def world(self):
print('world')

#create an object
obj=Computer()
print(obj.i)
print(obj.s)
obj.hello()
obj.hii()
obj.world()


output-

10
This is class
hello
Hii
world


-In this program, we create a class and three member functions, then we create an object and called that function
-Here self is use for the binding purpose, when need not to add additional variable to take a argument of the object.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!