Melody Anoni
Melody Anoni

Reputation: 243

Object has no attribute - Variable name shared between classes

I need to assign a new account to a SalesRep while also updating my Account's class to have Leslie as it's sales rep.

I recieve the error:

'str' object has no attribute 'set_sales_rep'

When I test the following lines:

rep_1 = SalesRep('Leslie', 'Knope', ['acct1', 'acct2', 'acct3'])
print(rep_1.add_account('acct4'))

SalesRep class:

class SalesRep(object):

    def __init__(self, first_name, last_name, accounts=None):
        self.first_name = first_name
        self.last_name = last_name
        self._accounts = []

        if accounts:
            self._accounts.extend(accounts)

    def __str__(self):
        return "{self.first_name} {self.last_name}".format(self=self)

    def get_accounts(self):
        return self._accounts

    def add_account(self, account):
        self._accounts.append(account)
        account.set_sales_rep(self)

Account class:

class Account(object):

    def __init__(self, name, sales_rep=None, segments=None):
        self.name = name
        self._sales_rep = sales_rep
        self._segments = []

        if segments:
            self._segments.extend(segments)

    def __str__(self):
        return "{self.name}".format(self=self)

    def get_sales_rep(self):
        return self._sales_rep

    def set_sales_rep(self, sales_rep):
        self._sales_rep = sales_rep

All I'm trying to do is test that add_account in the SalesRep class is working, I have 'set_sales_rep' defined in the Account class. I'm unsure if the error is that I'm relating 'set_sales_rep' incorrectly between the classes or my print() function call at the top is wrong.

Upvotes: 1

Views: 61

Answers (1)

iantbutler
iantbutler

Reputation: 451

rep_1 = SalesRep('Leslie', 'Knope', ['acct1', 'acct2', 'acct3'])
print(rep_1.add_account('acct4'))

You are passing a string to add_account instead of an account object.

Create an account object, acc = Account(name="acc4"), and then pass it to add_account, rep_1.add_account(acc).

Upvotes: 1

Related Questions