Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ebfc346

Browse files
Merge pull request #1 from ishanvipandey/patch-1
Update 01_Object_as_an_Argument.MD
2 parents 38f3ae4 + f5296f7 commit ebfc346

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

‎Lecture_17/01_Object_as_an_Argument.MD

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,61 @@ You can pass objects as arguments to functions or methods. This allows you to ma
66
- **Function Definition**: You define a function that takes an object as a parameter.
77
- **Accessing Object Attributes and Methods**: Inside the function, you can access the object's attributes and call its methods.
88

9+
**Example 1: Passing an Object to a Function**
10+
```py
11+
class Person:
12+
def __init__(self, name, age):
13+
self.name = name
14+
self.age = age
15+
16+
def greet(self):
17+
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
18+
19+
# Function that takes a Person object as an argument
20+
def introduce_person(person):
21+
print(f"Introducing a new person:")
22+
person.greet()
23+
24+
# Creating a Person object
25+
john = Person("John", 30)
26+
27+
# Passing the Person object to the function
28+
introduce_person(john)
29+
30+
"""
31+
Output:
32+
Introducing a new person:
33+
Hello, my name is John and I am 30 years old.
34+
"""
35+
```
36+
37+
**Example 2: Modifying an Object Inside a Function**
938

1039
```py
40+
class Dog:
41+
def __init__(self, name, breed):
42+
self.name = name
43+
self.breed = breed
44+
45+
def bark(self):
46+
print(f"{self.name} says woof!")
47+
48+
# Function that modifies the Dog object
49+
def change_dog_name(dog, new_name):
50+
dog.name = new_name
51+
52+
# Creating a Dog object
53+
my_dog = Dog("Rex", "German Shepherd")
54+
55+
# Changing the dog's name by passing the object to the function
56+
change_dog_name(my_dog, "Max")
57+
58+
# Checking the modified name
59+
print(my_dog.name) # Output: Max
60+
1161

12-
```
62+
"""
63+
Output:
64+
Max
65+
"""
66+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /