Write a python program to pass objects as arguments and return objects from function


The python program defines a MyClass class and a function process_object. The function takes an object of MyClass as an argument, modifies the object by doubling its value, and returns the modified object. Here's a breakdown of the code:

  • class MyClass: This class represents an object with an attribute value.
  • __init__(self, value): The constructor initializes a MyClass object with the provided value.
  • def process_object(obj): This function takes an object obj as an argument, doubles the value attribute of the object, and then returns the modified object.
  • An instance of MyClass is created, and my_obj is initialized with a value of 10.
  • The process_object function is called with my_obj as an argument. It modifies my_obj by doubling its value attribute, and the modified object is returned and assigned to result_obj.
  • The code prints the value attribute of both the original object (my_obj) and the modified object (result_obj).

Here's what the code does:

  • It creates an object of MyClass with an initial value of 10.
  • It calls the process_object function, which modifies the object by doubling its value.
  • It prints the value attribute of both the original and modified objects.

Source Code

class MyClass:
	def __init__(self, value):
		self.value = value
 
def process_object(obj):    
	obj.value *= 2	# Modify the object or perform some operations
	return obj	 # Return the modified object
 
my_obj = MyClass(10)	# Create an object of MyClass
 
result_obj = process_object(my_obj) # Call the function and pass the object as an argument
 
print("Original Object :",my_obj.value)
print("Modified Object :",result_obj.value)

Output

Original Object : 20
Modified Object : 20

Example Programs