You're pretty close here, but you have two problems.
First, your add_student
and remove_student
methods are trying to mutate some dictionary named self.student
, but you forgot to create it. Do that in the __init__
method, like this:
def __init__[self, courseName, capacity]:
# existing stuff
self.student = {}
Next, your add_student
needs to actually take a value
parameter if you want to use it:
def add_student[self, key, value]:
self.student[key] = value
And that's it. Now you can write code like this:
johnsmith = student['John', 'Smith', 14, 3.5]
intropython = course['Python 1', 20]
intropython.add_student['John Smith', johnsmith]
Of course you'll probably want to add more stuff to this later—e.g., a way
to get a default key for a student [like his first and last names], checking whether len[self.student] == self.capacity
before adding another student, etc. But this should get you started.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Prerequisite: Basics of OOPs in Python
In this article, we will learn how to access object methods and attributes within other objects in Python. If we have two different classes and one of these defined another class on calling the constructor. Then, the method and attributes of another class can be accessed by first class objects [ i.e; objects within objects ].
Here in the below example we learn to access object [its methods and attributes] within an object. We define two classes [class first and class second] with proper definitions.
- First class consists of a constructor and a method.
- The constructor form an object of the second class in the attribute of the first class.
- The method defines the presence in first class method.
- Similarly, the second class consists of a constructor and a method.
- The constructor form an attribute.
- The method defines the presence in the second class method.
As the attribute of first class work as an object of the second class so all the methods and attributes of the second class can be accessed using this:
object_of_first_class.attribute_of_first_class
Below is the implementation:
Python3
class
first:
def
__init__[
self
]:
self
.fst
=
second[]
def
first_method[
self
]:
print
[
"Inside first method"
]
class
second:
def
__init__[
self
]:
self
.snd
=
"GFG"
def
second_method[
self
]:
print
[
"Inside second method"
]
obj1
=
first[]
print
[obj1]
obj2
=
obj1.fst
print
[obj2]
print
[obj2.snd]
obj2.second_method[]
Output:
GFG Inside second method
Copy an Object in Python
In Python, we use =
operator to create a copy of an object. You may think that this creates a new object; it doesn't. It only creates a new variable that shares the reference of the original object.
Let's take an example where we create a list named old_list and pass an object reference to new_list using =
operator.
Example 1: Copy using = operator
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 'a']]
new_list = old_list
new_list[2][2] = 9
print['Old List:', old_list]
print['ID of Old List:', id[old_list]]
print['New List:', new_list]
print['ID of New List:', id[new_list]]
When we run above program, the output will be:
Old List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ID of Old List: 140673303268168 New List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] ID of New List: 140673303268168
As you can see from the output both variables old_list and new_list shares the same id i.e 140673303268168
.
So, if you want to modify any values in new_list or old_list, the change is visible in both.
Essentially, sometimes you may want to have the original values unchanged and only modify the new values or vice versa. In Python, there are two ways to create copies:
- Shallow Copy
- Deep Copy
To make these copy work, we use the copy
module.
Copy Module
We use the copy
module of Python for shallow and deep copy operations. Suppose, you need to copy the compound list say x. For example:
import copy copy.copy[x] copy.deepcopy[x]
Here, the copy[]
return a shallow copy of x. Similarly, deepcopy[]
return a deep copy of x.
Shallow Copy
A shallow copy creates a new object which stores the reference of the original elements.
So, a shallow copy doesn't create a copy of nested objects, instead it just copies the reference of nested objects. This means, a copy process does not recurse or create copies of nested objects itself.
Example 2: Create a copy using shallow copy
import copy
old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy[old_list]
print["Old list:", old_list]
print["New list:", new_list]
When we run the program , the output will be:
Old list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] New list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In above
program, we created a nested list and then shallow copy it using copy[]
method.
This means it will create new and independent object with same content. To verify this, we print the both old_list and new_list.
To confirm that new_list is different from old_list, we try to add new nested object to original and check it.
Example 3: Adding [4, 4, 4] to old_list, using shallow copy
import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.copy[old_list]
old_list.append[[4, 4, 4]]
print["Old list:", old_list]
print["New list:", new_list]
When we run the program, it will output:
Old list: [[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4]] New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
In the above program, we created a shallow copy of old_list. The new_list contains references to original nested objects stored in old_list. Then we add the new list i.e [4, 4, 4]
into old_list. This new sublist was not copied in new_list.
However, when you change any nested objects in old_list, the changes appear in new_list.
Example 4: Adding new nested object using Shallow copy
import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.copy[old_list]
old_list[1][1] = 'AA'
print["Old list:", old_list]
print["New list:", new_list]
When we run the program, it will output:
Old list: [[1, 1, 1], [2, 'AA', 2], [3, 3, 3]] New list: [[1, 1, 1], [2, 'AA', 2], [3, 3, 3]]
In the above program, we made changes to old_list i.e old_list[1][1] = 'AA'
. Both sublists of old_list and new_list at index [1][1]
were modified. This is because, both lists share the reference of same nested objects.
Deep Copy
A deep copy creates a new object and recursively adds the copies of nested objects present in the original elements.
Let’s
continue with example 2. However, we are going to create deep copy using deepcopy[]
function present in copy
module. The deep copy creates independent copy of original object and all its nested objects.
Example 5: Copying a list using deepcopy[]
import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.deepcopy[old_list]
print["Old list:", old_list]
print["New list:", new_list]
When we run the program, it will output:
Old list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]] New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
In the above program, we use deepcopy[]
function to create copy which looks similar.
However, if you make changes to any nested objects in original object old_list, you’ll see no changes to the copy new_list.
Example 6: Adding a new nested object in the list using Deep copy
import copy
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.deepcopy[old_list]
old_list[1][0] = 'BB'
print["Old list:", old_list]
print["New list:", new_list]
When we run the program, it will output:
Old list: [[1, 1, 1], ['BB', 2, 2], [3, 3, 3]] New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
In the above program, when we assign a new value to old_list, we can see only the old_list is modified. This means, both the old_list and the new_list are independent. This is because the old_list was recursively copied, which is true for all its nested objects.