Home > Mobile >  How to instantiate an object which belongs to another class without importing
How to instantiate an object which belongs to another class without importing

Time:10-11

enter image description hereI am working on the second part of my Bank management system project in Python. I have 4 modules created: person.py, bank_account.py, bank_card.py and main.py

I also have 3 classes created for each of the following modules: Person Class, Bank Account Class and Bank Card class.

I am being asked to create a new_card class function under Bank_Account class. To do so, I have imported (because I am allowed here) the Bank Card class into the bank_account.py module. No problem at this point. This is the code used for my new_card function within my Bank_Account class:

def new_card (self,card_number, month, year, cvv, pin_code):
    card1 = Bank_Card(card_number,month,year,cvv,pin_code)
    self.__bank_card = card1
    return card1

Where I am struggling is when the task request to create a card object in main.py. I am not allowed to import the Bank Card to main.py but instead I need to create the card object using the function new_card defined on Bank_Account class. When I am trying to do so by trying to create card2 in main.py, I cannot call the new_card function like card2.new_card since the Bank_Card class was not imported :(

Any ideas?

I have added a screenshot of how my code looks so far... If I do it as proposed (calling the method Bank_Account.new_card(), then I get an error saying that there are missing arguments.

I hope the picture helps you to clarify a little bit more the issue I am facing

CodePudding user response:

You do not need to import Bank_Card class in you main program as it is indirectly available in the Bank_Account class. From what I can understand from the question, all you need to do is import the Bank_Account class to the main program from "bank_account.py" module.

This is how you could achieve the same:

## File: "main.py"

# Import the Bank Account class from the module
from bank_account import Bank_Account

# Define the card details which will be used for creating the card
# You may provide the details inline to the method call if you want

card_number = ...
month = ...
year = ...
cvv = ...
pin_code = ...

# Create a new bank card instance by calling the `new_card` method
my_card = Bank_Account.get_card(card_number, month, year, cvv, pin_code)

Here, we are importing the Bank_Account class from bank_account.py module and then calling the new_card() method that was created on the class. This will return a new Bank Card instance which you can use as intended. Unfortunately, it will throw an error when calling Bank_Account.get_card() method, since new_card is not a static method. In order to fix that, you need to decorate the new_card method as a static method like so:

class Bank_Account:
    ...

    @staticmethod
    def new_card(card_number, month, year, cvv, pin_code):
        ...

Note that the self parameter is removed from the new_card() method since it does not need an object of Bank_Account class but could instead be called directly using the class itself.

  • Related