OO设计原则是否适用于Python? [英] Do OO design principles apply to Python?

查看:94
本文介绍了OO设计原则是否适用于Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎许多面向对象的讨论都使用Java或C#作为示例(例如Head First设计模式).

It seems like many OO discussions use Java or C# as examples (e.g. Head First Design Patterns).

这些模式是否同样适用于Python?或者,如果我遵循设计模式,我是否最终会用Python编写Java(这显然是很不好的事情)?

Do these patterns apply equally to Python? Or if I follow the design patterns, will I just end up writing Java in Python (which apparently is a very bad thing)?

推荐答案

最大的区别是Python是鸭子类型的,这意味着您不需要像Java那样详细地计划类层次结构,并且具有第一个类功能.例如,当您可以传入一个函数而不是仅仅为了模拟高阶函数而必须创建接口等时,策略模式就变得更加简单和明显.更一般而言,Python具有许多常见设计模式的语法糖,例如迭代器和上述策略.理解这些模式可能很有用(我已经读过《 Head First》,发现它非常有用),但是请考虑使用Python的方法来实现它们,而不是像在Java中那样做事.

The biggest differences are that Python is duck typed, meaning that you won't need to plan out class hierarchies in as much detail as in Java, and has first class functions. The strategy pattern, for example, becomes much simpler and more obvious when you can just pass a function in, rather than having to make interfaces, etc. just to simulate higher order functions. More generally, Python has syntactic sugar for a lot of common design patterns, such as the iterator and the aforementioned strategy. It might be useful to understand these patterns (I've read Head First and found it pretty useful), but think about Pythonic ways to implement them rather than just doing things the same way you would in Java.

这篇关于OO设计原则是否适用于Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆