0

I'm trying to figure out how to properly append to the friends list initialised in the constructor and have tried a few different angles. I know that self.friends.append(SocialNetworkUser) is the issue, but not sure how to properly modify.

class SocialNetworkUser:

    potentialFriends = []

    def __init__(self, name, friends=[]):
        self.name = name

    def befriend(self, SocialNetworkUser):
        self.potentialFriends.append(SocialNetworkUser)
        if self in SocialNetworkUser.potentialFriends:
            self.friends.append(SocialNetworkUser)
DanielSon
  • 1,415
  • 4
  • 27
  • 40
  • You probably should start with not using a class name as a parameter name – OneCricketeer Nov 09 '16 at 09:03
  • I'm trying to take an instance of the same class type as the argument for the method. Like take self and another SocialNetworkUser as the arguments and then compare/modify each others friends lists – DanielSon Nov 09 '16 at 09:03
  • That's fine and all, but that isn't what your code is doing. The parameter name doesn't have to match the class. Also, `self.potentialFriends` isn't the correct reference to `SocialNetworkUser.potentialFriends` and `self.friends` is never initialize despite what you're trying to do with `def __init__(self, name, friends=[]):` – OneCricketeer Nov 09 '16 at 09:06
  • 2
    For starters, you need `self.friends = []` in `__init__`. Then read http://stackoverflow.com/q/1132941/476. Finally, this whole deal with `potentialFriends` seems somewhat nonsensical; but I'll leave that up to you. – deceze Nov 09 '16 at 09:06
  • Thanks for tips guys, relatively new to classes so will look into it more – DanielSon Nov 09 '16 at 09:08

0 Answers0