python嵌套类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14606559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python nested classes
提问by Daquicker
First of all, here's my test code, I'm using python 3.2.x:
首先,这是我的测试代码,我使用的是 python 3.2.x:
class account:
def __init__(self):
pass
class bank:
def __init__(self):
self.balance = 100000
def balance(self):
self.balance
def whitdraw(self, amount):
self.balance -= amount
def deposit(self, amount):
self.balance += amount
when I do:
当我做:
a = account()
a.bank.balance
I expected to get the value of balance returned, instead I get the function "balance", why is this? It returns the value of balance when I do:
我希望得到返回的 balance 值,而不是得到函数“balance”,这是为什么?当我这样做时,它返回 balance 的值:
class bank:
def __init__(self):
self.balance = 100000
def balance(self):
self.balance
def whitdraw(self, amount):
self.balance -= amount
def deposit(self, amount):
self.balance += amount
a = bank()
a.balance
So I want to know why this is and it would be great if someone could come up with a way to give me the value of balance in the nested version.
所以我想知道这是为什么,如果有人能想出一种方法来给我嵌套版本中的平衡值,那就太好了。
采纳答案by codeape
My version of your code, with comments:
我的代码版本,带有注释:
#
# 1. CamelCasing for classes
#
class Account:
def __init__(self):
# 2. to refer to the inner class, you must use self.Bank
# 3. no need to use an inner class here
self.bank = self.Bank()
class Bank:
def __init__(self):
self.balance = 100000
# 4. in your original code, you had a method with the same name as
# the attribute you set in the constructor. That meant that the
# method was replaced with a value every time the constructor was
# called. No need for a method to do a simple attribute lookup. This
# is Python, not Java.
def withdraw(self, amount):
self.balance -= amount
def deposit(self, amount):
self.balance += amount
a = Account()
print(a.bank.balance)
回答by NPE
There are several problems:
有几个问题:
- You're using the name
balancefor both the data member and for the function. - You're missing a
returnstatement inbalance(). balance()operates on an instanceofbank. There is no instance ina.bank.balance: here,a.bankrefers to the inner class itself.
- 您正在
balance为数据成员和函数使用名称。 - 您
return在balance(). balance()对 的实例进行操作bank。a.bank.balance: 这里没有实例,a.bank指的是内部类本身。
回答by mgilson
a.bankis the class(not instance) since you've never created an instanceof the bank on a. So if a.bankis a class, a.bank.balanceis a method bound to that class.
a.bank是类(不是实例),因为您从未在 上创建银行的实例a。所以如果a.bank是一个类,a.bank.balance是一个绑定到该类的方法。
This works however:
然而,这有效:
class account:
def __init__(self):
self.bank = account.bank()
class bank:
def __init__(self):
self.balance = 100000
def whitdraw(self, amount):
self.balance -= amount
def deposit(self, amount):
self.balance += amount
a = account()
print a.bank.balance
Of course, as you show working code without nested classes, It really begs the question about whyyou want to use nested classes for this. I would argue that the non-nested version is much cleaner.
当然,当您展示没有嵌套类的工作代码时,它确实回避了为什么要为此使用嵌套类的问题。我认为非嵌套版本要干净得多。

