#๐Ÿ”’ Accessing object of class created within another object with inheritance

40 messages ยท Page 1 of 1 (latest)

primal plinth
#

I know it is kind of a confusing title, but I am doing a university assignment where we have to code a banking/account managing system. For this we create three classes, one for the Customer, one for Accounts and one for the Bank where the customers and accounts are stored. Within the bank class there are commands for adding customers and accounts, for which I have used the inheritance function to just create an object of the class account/customer. I am unsure whether I have implemented this right though, since I am now trying to access these accounts or customers but have no idea how, since they are created within the bank class. I will add my code for the classes, and if anything is unclear just ask me and I will try to explain!

https://paste.pythondiscord.com/AOTQ

hexed salmonBOT
#

@primal plinth

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

last spade
#

You have indeed inherited from Account and Customer, which means Bank is now an Account and bank is a Customer, which is probably not what you wanted @primal plinth

#

You don't need to inherit from them. You can f.e. make an attribute self.customers in the init of the bank which is an empty list. Then you add a method like add_customer, which appends a customer object to this list.

#
class Customer:
  def __init__(self, name: str, age: int) -> None:
    self.name = name
    self.age = age


class Bank:
  def __init__(self) -> None:
    self.customers: list[Customer] = []
    self.customer_count = 0  # Just for show 

  def add_customer(self, new_customer: Customer) -> None:
    self.customers.append(new_customer)
    self.customer_count += 1
#

Here's a small example

#

Let me know if this makes sense and if you need more help @primal plinth

#

You are also using dict in your code (initialized with {}), but you seem to call them lists.

primal plinth
#

I know, I started using lists but then realized I had to use dict ๐Ÿ˜…

#

so like creating customers and putting them in a list? Is that essentially what you are saying?

last spade
#

Yeah. This problem does not really require inheritance. Inheritance makes it a "A is B" relation. But Bank is not Customer, so it does not make sense.

#

Instead, Bank has customers. So you can simply contain them in a list that Bank has acess to

#

or a dict

primal plinth
#

Inheritance is a big part of the like course curriculim, so I thought I had to use it somewhere in this assignment

#

but what youre saying makes a lot of sense

#

I wasn't trying to make the whole Bank class a Customer, that's why I put it under the add customer function. I thought that initalizing the customer under this function would only create a customer if the function was used

last spade
#

class Bank(Customer, Account): That's the line that states Bank is a sub-class of Customer & Account

#

So simply make it class Bank:

#

The () is also not necessary

primal plinth
#

okay

#

thank you

#

is the way you showed the only way to create a customer? Or can I also write "Customer(customer_id, personal_nbr)" under the function of add customer?

#

Sorry for the screenshot I just don't know how to format the code in discord

last spade
#

In my case, I expect that a Customer object is made, and then the method is called to add it to bank

#

That way bank is not responsible for customer creation

#

But you could of course also create a method like:

class Customer:
  def __init__(self, name: str, age: int) -> None:
    self.name = name
    self.age = age


class Bank:
  def __init__(self) -> None:
    self.customers: list[Customer] = []
    self.customer_count = 0  # Just for show 

  def add_customer(self, name: str, age: int) -> None:
    new_customer = Customer(name, age)
    self.customers.append(new_customer)
    self.customer_count += 1
primal plinth
#

Okay

#

So i can't create an object by just specifying the parameters, like in the screenshot I sent? I have to name it something?

#

Cause later on I have to access the account to put in money, and then I need access to the account in some way, either through the name of the account or its parameters, and wouldn't your example have the same name, i.e new_customer, for every customer created?

#

For the customer this ofc is not relevant, but if i use the code above for the account as well I wouldn't know how to access it

last spade
#

If you want we could also call rq. I can maybe just explain you a little more about classes and inheritance and stuff

#

For your latter question, you can access something from bank like

...

my_bank = Bank(...)  # Fill in arguments
my_bank.add_customer(name="John", age=55)
my_bank.add_customer(name="Clara", age=42)

customer1 = my_bank.customers[0]
primal plinth
#

Hmm, no I think I know what you are saying

#

sorry am a bit too shy to call

#

But I think I will be using a dictionary instead of a list, since that makes it easier to specify exactly which account number I need etc.

#

But thank you so much for your help!

hexed salmonBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.