我有2节课(例如)
class FilePath(Path):
pass
class DirectoryPath(Path):
pass
我选择哪个被makepath()方法实例化,simplified:
def makepath(string):
if os.path.isfile(string):
return FilePath(string)
return DirectoryPath(string)
我已经多年不喜欢make\u xxx()方法了。它让我想起了太多的Java工厂,所以我想知道是否还有一个更像python的成语?你知道吗
在这种情况下,“
path
”听起来不错。 由于makepath方法返回或目录或文件路径,所以just path看起来更一般、更简短!你知道吗还有其他解决方案(比如重写
Path.__new__
),但是为了便于说明,下面是一个Q&D示例,使用
abc
查找适当的具体子类,而不在基类中对子类进行harcoding:相关问题 更多 >
编程相关推荐