Python Demonstrate an example of Class and Object

Here, we will show an example of class and item in Python. How to characterize a class, pronounce an item and use it?

Make a class, and the techniques to deal with string, such as string task with “None“, hard coded worth, with contention in Python.

In this example, there are the following strategies:

init(self)

Much the same as a constructor in OOPS idea, this strategy summons, when the item will be made. In this example, string variable msg will be relegated by “None“.

assignValue(self)

This strategy will relegate “Hello world” to msg.

getValue(self,str)

By utilizing this strategy, variable msg will be relegated through the worth which will be passed from technique call.

printValue(self)

This technique will print the estimation of variable msg.

Program:

# Python program to demonstrate an 
# example of class

class Message(object):
	def __init__(self):
		# assign none to variable 
		self.msg = None

	def assignValue(self):
		# assign any value 
		self.msg = "Hello World"

	def getValue (self,str):
		# assign variable with parameter 
		self.msg = str

	def printValue(self):
		# print the value 
		print "msg = ",self.msg
	
# Main code
# creating object of the class 
# Here, M is object nae
M = Message()

# print value 
print "value after init. the object..."
M.printValue();

# assign value 
M.assignValue() 

# print value 
print "value after assignValue ()...."
M.printValue();

# assign value using arguemnt
M.getValue("How are you?")
print "value after getValue ()..."
M.printValue();

Output:

value after init. the object...
msg =  None
value after assignValue ()....
msg =  Hello World
value after getValue ()...
msg =  How are you?

Leave a Comment

error: Alert: Content is protected!!