如何在不导入的情况下实例化属于另一个 class 的对象
How to instantiate an object which belongs to another class without importing
我正在 Python 进行银行管理系统项目的第二部分。
我创建了 4 个模块:person.py、bank_account.py、bank_card.py 和 main.py
我还为以下每个模块创建了 3 个 classes:个人 Class、银行帐户 Class 和银行卡 class。
我被要求在 Bank_Account class 下创建一个 new_card class 函数。为此,我已将银行卡 class 导入(因为我在这里被允许)到 bank_account.py 模块中。在这一点上没问题。这是我的 Bank_Account class:
中用于我的 new_card 函数的代码
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
当任务请求在 main.py 中创建卡片对象时,我遇到了困难。我不允许将银行卡导入main.py,但我需要使用 Bank_Account class 上定义的函数 new_card 创建卡片对象。当我尝试通过在 main.py 中创建 card2 时,我无法像 card2.new_card 那样调用 new_card 函数 因为 Bank_Card class 没有导入 :(
有什么想法吗?
我已经添加了到目前为止我的代码看起来如何的屏幕截图...如果我按照建议执行(调用方法 Bank_Account.new_card()),那么我会收到一条错误消息,指出缺少参数。
我希望这张图片能帮助你更清楚地说明我面临的问题
您不需要在主程序中导入 Bank_Card
class,因为它可以在 Bank_Account
class 中间接使用。据我从问题中可以理解,您需要做的就是将 Bank_Account
class 从“bank_account.py”模块导入主程序。
这就是您可以达到相同目的的方法:
## 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)
在这里,我们从 bank_account.py
模块导入 Bank_Account
class,然后调用在 class 上创建的 new_card()
方法。这将 return 一个您可以按预期使用的新银行卡实例。不幸的是,它会在调用 Bank_Account.get_card()
方法时抛出错误,因为 new_card
不是静态方法。为了解决这个问题,您需要将 new_card
方法装饰为静态方法,如下所示:
class Bank_Account:
...
@staticmethod
def new_card(card_number, month, year, cvv, pin_code):
...
请注意,self
参数已从 new_card()
方法中删除,因为它不需要 Bank_Account
class 的对象,而是可以直接使用class本身。
我还为以下每个模块创建了 3 个 classes:个人 Class、银行帐户 Class 和银行卡 class。
我被要求在 Bank_Account class 下创建一个 new_card class 函数。为此,我已将银行卡 class 导入(因为我在这里被允许)到 bank_account.py 模块中。在这一点上没问题。这是我的 Bank_Account class:
中用于我的 new_card 函数的代码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
当任务请求在 main.py 中创建卡片对象时,我遇到了困难。我不允许将银行卡导入main.py,但我需要使用 Bank_Account class 上定义的函数 new_card 创建卡片对象。当我尝试通过在 main.py 中创建 card2 时,我无法像 card2.new_card 那样调用 new_card 函数 因为 Bank_Card class 没有导入 :(
有什么想法吗?
我已经添加了到目前为止我的代码看起来如何的屏幕截图...如果我按照建议执行(调用方法 Bank_Account.new_card()),那么我会收到一条错误消息,指出缺少参数。
我希望这张图片能帮助你更清楚地说明我面临的问题
您不需要在主程序中导入 Bank_Card
class,因为它可以在 Bank_Account
class 中间接使用。据我从问题中可以理解,您需要做的就是将 Bank_Account
class 从“bank_account.py”模块导入主程序。
这就是您可以达到相同目的的方法:
## 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)
在这里,我们从 bank_account.py
模块导入 Bank_Account
class,然后调用在 class 上创建的 new_card()
方法。这将 return 一个您可以按预期使用的新银行卡实例。不幸的是,它会在调用 Bank_Account.get_card()
方法时抛出错误,因为 new_card
不是静态方法。为了解决这个问题,您需要将 new_card
方法装饰为静态方法,如下所示:
class Bank_Account:
...
@staticmethod
def new_card(card_number, month, year, cvv, pin_code):
...
请注意,self
参数已从 new_card()
方法中删除,因为它不需要 Bank_Account
class 的对象,而是可以直接使用class本身。