Unexplainable behaviour when defining a method in a class

Hi everybody!
I’m trying to do the excercise from the lesson on OOP.

We are supposed to:

  1. Define an animal class that has a friends attribute , which is a list of all of that animal’s friends.
  2. Define a method that takes as an argument another object of the same class and appends its name to the current’s animal friend list and vice versa.

I did the exercise using a Turtle type, and got an unusual behavior that I could not understand.

In short, I defined a make_friends method that can be applied on a Turtle object. This method takes a seond Turtle object as an argument and appends each turtle’s name to the other one’s friend list.
HOWEVER, for some reason, this results in both turtles being added to each ones list instead of to just the other turtle’s list, so that I end up with two identical lists for each turtle.

This is the code I wrote:

class Turtle:
def init(self,name,friends=,is_sea=False):
self.name=name
self.is_sea=is_sea
self.friends=friends

def become_friend(self,friend):
    if type(friend)!=Turtle:
        print("Friend must also be of type \"Turtle\"")
    else:
        if friend.is_sea!=self.is_sea:
            print("Friend must be same type of turtle, sea and land turtles don't get along!!")
        else:
            self.friends.append(friend.name)
            friend.friends.append(self.name)

Rafael=Turtle(“Rafael”)
Leonardo=Turtle(“Leonardo”)
Leonardo.become_friend(Rafael)
print(Leonardo.friends)
print(Rafael.friends)

I expected to get:
[‘Rafael’]
[‘Leonardo’]

But instead got:
[‘Rafael’, ‘Leonardo’]
[‘Rafael’, ‘Leonardo’]

Thanks!

Using [] as the default for a parameter can lead to unexpected behavior (like using the same list every time that function or method is called).

Here’s a workaround:

  def init(self, name, friends=None, is_sea=False):
    if friends is None: 
      friends = []
3 Likes

Thanks!
So just to make sure- using as the default value of a variable is problematic in all cases (i.e., in any type of function? or just within classes).

Its for any type of function, not just a method. (A method is a function in a class or for an object.)

2 Likes

I like that distinction. A method acts for an object; a function acts on an object.