【发布时间】:2016-06-17 20:18:32
【问题描述】:
今天我正在试验和/或玩弄 Python 3.5。
我碰巧“不小心”这样写:
class Apple(object):
class green(object):
def eat():
print("You just ate a green apple...")
class red(object):
def eat():
print("You just ate a red apple...")
意思是我现在可以做:
Apple.red.eat() # would print: "You just are a red apple..."
Apple.green.eat() # would print: "You just are a green apple..."
这让我问,os.path.exists() 之类的函数是如何创建的,还是使用另一个“方法”创建的?
这应该被修复,还是故意的?
提前谢谢你。
【问题讨论】:
-
我不确定,但我认为
import os会从os.py... @ 必须是path类中的function。 -
os.path是一个子模块,os是一个包。 -
如果
os.path是一个类,那么您可以创建os.path的实例,这没有任何意义,您的代码也存在同样的问题。执行x = Apple.red() ; x.eat()会引发错误,因为eat不是有效方法(缺少self参数)
标签: python class python-3.x inner-classes python-3.5