You might want to do this when the "inner" class is a one-off, which will never be used outside the definition of the outer class. For example to use a metaclass, it's sometimes handy to do
class Foo(object):
class __metaclass__(type):
....
instead of defining a metaclass separately, if you're only using it once.
The only other time I've used nested classes like that, I used the outer class only as a namespace to group a bunch of closely related classes together:
class Group(object):
class cls1(object):
...
class cls2(object):
...
Then from another module, you can import Group and refer to these as Group.cls1, Group.cls2 etc. However one might argue that you can accomplish exactly the same (perhaps in a less confusing way) by using a module.
Answer from dF. on Stack OverflowI'm fairly new to OOP in Python, and am toying with the idea of making a 2D world that appears 3D by moving things at different rates. Simple enough.
But I find myself wondering if there are times when defining classes within classes is useful. For example, a "layer" object might be something defined within a "viewer" object.
If things are done this way, are there ways for a nested class to have access to methods or attributes of an overarching class? (I was thinking that the "layers" might all have access to where the "viewer" is so that they know how to present themselves. Each "layer" would calculate this for itself, since parallax would change based on apparent distance of that "layer" from the player, but it would all be based on positional attributes the "viewer" has.)
Or is there some better way to organize this? I'm curious to know what kind of organizational decisions people might make and why.
Thank you for sharing your thoughts and/or resources on this.
oop - Is there a benefit to defining a class inside another class in Python? - Stack Overflow
python - What are the consequences of nesting classes? - Stack Overflow
Python inner classes inheritance from parent class
python nested classes - Stack Overflow
When should I use a nested class?
Can Python classes be defined inside other classes?
What is an alternative to nested classes?
You might want to do this when the "inner" class is a one-off, which will never be used outside the definition of the outer class. For example to use a metaclass, it's sometimes handy to do
class Foo(object):
class __metaclass__(type):
....
instead of defining a metaclass separately, if you're only using it once.
The only other time I've used nested classes like that, I used the outer class only as a namespace to group a bunch of closely related classes together:
class Group(object):
class cls1(object):
...
class cls2(object):
...
Then from another module, you can import Group and refer to these as Group.cls1, Group.cls2 etc. However one might argue that you can accomplish exactly the same (perhaps in a less confusing way) by using a module.
I don't know Python, but your question seems very general. Ignore me if it's specific to Python.
Class nesting is all about scope. If you think that one class will only make sense in the context of another one, then the former is probably a good candidate to become a nested class.
It is a common pattern make helper classes as private, nested classes.
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)
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 instance ofbank. There is no instance ina.bank.balance: here,a.bankrefers to the inner class itself.