Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

[Sugestão] Getter e setters em Account e Client

Vamos em dois posts

from client import Client


class Account:
    accounts = {}

    # initialization
    def __init__(self, number, holder: Client, balance, limit=1000):
        self.__number = number
        self.__holder = holder
        self.__balance = balance
        self.__limit = limit
        # Account.accounts.append(self) # list
        Account.accounts[number] = self  # dictionary

    # dunder methods
    def __str__(self):
        return f"Account: {self.__number}, Holder: {self.__holder}, Balance: {self.__balance}, Limit: {self.__limit}"

    # getter properties
    @property
    def number(self):
        return self.__number

    @property
    def holder(self):
        return self.__holder

    @property
    def balance(self):
        return self.__balance

    @property
    def limit(self):
        return self.__limit

    # setter properties
    @limit.setter
    def limit(self, value):
        if value < 0:
            raise ValueError("You can't have a negative limit.")
        else:
            self.__limit = value

    # utility functions
    @staticmethod
    def format_value(value, account_number=None):
        if account_number is not None and account_number == value:
            return str(value)

        if isinstance(value, float) or not isinstance(value, str):
            return "US${:,.2f}".format(value)
        else:
            return value

    # instance methods
    def client_info(self):
        result = "Here's the client's information:\n"
        account_info = {
            "number": self.number,
            "holder": self.holder,
            "balance": self.format_value(self.balance),
            "limit": self.format_value(self.limit)
        }
        for index, (key, value) in enumerate(account_info.items(), start=1):
            result += f"{index}) {key}: {value}\n"
        return result

    def print_client_info(self):
        print(self.client_info())

    def deposit(self, value):
        if value > 0:
            self.__balance += value
        else:
            print("You must deposit a positive value.")

    def withdraw(self, value):
        if value > 0:
            self.__balance -= value
        else:
            print("The withdrawal amount must be a positive value.")

    def transfer(self, value, origin, destination):
        if origin == destination or origin.balance < value or value <= 0:
            print("Invalid transfer", "\n")
            return

        try:
            origin.withdraw(value)
            destination.deposit(value)
        except Exception as e:
            print("Transfer error: ", e)

    def is_delinquent(self):
        return self.balance < 0

    def what_to_do_with_client(self):
        if self.balance > 0:
            print(f"{self.holder}'s in good standing.")
        if self.balance < -abs(self.limit):
            print("Looks like we're gonna have to throw him in a dungeon.")
        elif abs(self.balance) == -abs(self.limit):
            print("Living dangerously, are we?")
        else:
            print("Let him go... for now...")

    # class methods
    @classmethod
    def print_client_list(cls):
        print("Here's a list of all our clients:\n")
        for index, account in enumerate(cls.accounts.values(), start=1):
            print(f"{index}) {account.holder}")
        print()

    @classmethod
    def print_delinquent_list(cls):
        print("Here's a list of all our delinquent clients:\n")
        for index, account in enumerate(cls.accounts.values(), start=1):
            if account.is_delinquent():
                print(f"{index}) {account.holder}")
                print()
                account.what_to_do_with_client()
                print()

    @classmethod
    def print_client_names(cls):
        accounts_list = list(cls.accounts.values())
        if len(accounts_list) > 2:
            first_two_names = ", ".join(account.holder.full_name
                                        for account in accounts_list[:2])
            last_name = accounts_list[-1].holder.full_name
            print(f"{first_two_names}, and {last_name}")
        elif len(accounts_list) == 2:
            print(f"{accounts_list[0].holder.full_name} and {accounts_list[1].holder.full_name}")
        elif accounts_list:
            print(accounts_list[0].holder.full_name)
        else:
            print("No account holders")

    @classmethod
    def total_balance(cls):
        total_balance = sum(account.balance
                            for account in cls.accounts.values())
        return cls.format_value(total_balance)

    @classmethod
    def total_limit(cls):
        total_limit = sum(account.limit for account in cls.accounts.values())
        return cls.format_value(total_limit)

(...)

1 resposta
solução!
class Client:

    def __init__(self, first_name, last_name):
        self.__first_name = first_name
        self.__last_name = last_name

    # dunder methods
    def __str__(self):
        return f"{self.full_name}"

    # getter properties
    @property
    def first_name(self):
        return self.__first_name.title()

    @property
    def last_name(self):
        return self.__last_name.title()

    @property
    def full_name(self):
        return f"{self.first_name} {self.last_name}"

    # setter properties
    @first_name.setter
    def first_name(self, first_name):
        self.__first_name = first_name

    @last_name.setter
    def last_name(self, last_name):
        self.__last_name = last_name

    @full_name.setter
    def full_name(self, full_name):
        first_name, last_name = full_name.split(" ", 1)
        self.__first_name = first_name
        self.__last_name = last_name

As instâncias estão em outros arquivos. Pode-se então testar mudanças em nomes no main.py assim

print(Account.accounts[321].holder)
Account.accounts[321].holder.first_name = "Beatrix"
print(Account.accounts[321].holder)
Account.accounts[321].holder.last_name = "Kiddo"
print(Account.accounts[321].holder)
Account.accounts[321].holder.full_name = "Yo Mama"
print(Account.accounts[321].holder)

e de fato funciona.