python设计模式-Python 实用宝典

python设计模式

关闭。此问题不符合堆栈溢出准则。它当前不接受答案。 想改善这个问题吗?更新问题,使其成为Stack Overflow 的主题。 4年前关闭。 改善这个问题 我正在寻找使用Python给出最佳实践,设计模式和SOLID原理示例的任何资源。

问题:python设计模式

我正在寻找使用Python给出最佳实践,设计模式和SOLID原理示例的任何资源。

I am looking for any resources that gives examples of Best Practices, Design patterns and the SOLID principles using Python.


回答 0

这些重叠

Python中级和高级软件木工

像Pythonista一样的代码:惯用的Python

Python成语与效率

Google美国开发人员日-Python设计模式

另一个资源是Python食谱中的示例。很多人没有遵循最佳实践,但是您可以在其中找到一些有用的模式

Some overlap in these

Intermediate and Advanced Software Carpentry in Python

Code Like a Pythonista: Idiomatic Python

Python Idioms and Efficiency

Google Developers Day US - Python Design Patterns

Another resource is by example at the Python Recipes. A good number do not follow best practices but you can find some patterns in there that are useful


回答 1

类型

>>> import this

在Python控制台中。

尽管这通常被当作一个笑话,但它包含几个有效的特定于Python的公理。

Type

>>> import this

in a Python console.

Although this is usually treated as a (fine!) joke, it contains a couple of valid python-specific axioms.


回答 2

布鲁斯·埃克尔(Bruce Eckel)的“ Python中思想”在很大程度上依赖于设计模式

Bruce Eckel's "Thinking in Python" leans heavily on Design Patterns


回答 3

您可以在这里这里开始。

要更深入地了解设计模式,您应该查看设计模式:可重用的面向对象软件的元素。源代码不是Python中的代码,但是不需要您了解这些模式。

You can get started here and here.

For a more in depth look at design pattners you should look at Design Patterns: Elements of Reusable Object-Oriented Software. The source code is not in Python, but it doesn't need to be for you to understand the patterns.


回答 4

在调用可能存在或不存在的对象的属性时,可以用来简化代码的方法是使用Null对象设计模式(在Python Cookbook中引入了Null对象设计模式)。

大致来说,使用Null对象的目标是为Python中常用的原始数据类型None或其他语言中的Null(或Null指针)提供“智能”替代。这些用于许多目的,包括重要的情况,在这种情况下,无论出于何种原因,一组其他类似元素的成员都是特殊的。通常,这会导致条件语句来区分普通元素和原始Null值。

这个对象只是吃了缺少属性错误,并且可以避免检查它们的存在。

无非就是

class Null(object):

    def __init__(self, *args, **kwargs):
        "Ignore parameters."
        return None

    def __call__(self, *args, **kwargs):
        "Ignore method calls."
        return self

    def __getattr__(self, mname):
        "Ignore attribute requests."
        return self

    def __setattr__(self, name, value):
        "Ignore attribute setting."
        return self

    def __delattr__(self, name):
        "Ignore deleting attributes."
        return self

    def __repr__(self):
        "Return a string representation."
        return "<Null>"

    def __str__(self):
        "Convert to a string and return it."
        return "Null"

这样一来,如果您这样做,Null("any", "params", "you", "want").attribute_that_doesnt_exists()它就不会爆炸,而只是默默地变成了pass

通常你会做类似的事情

if obj.attr:
    obj.attr()

这样,您只需执行以下操作:

obj.attr()

忘掉它 注意该Null对象的广泛使用可能会在代码中隐藏错误。

Something you can use to simplify your code when calling attributes on objects that might or might not exist is to use the Null Object Design Pattern (to which I was introduced in Python Cookbook).

Roughly, the goal with Null objects is to provide an 'intelligent' replacement for the often used primitive data type None in Python or Null (or Null pointers) in other languages. These are used for many purposes including the important case where one member of some group of otherwise similar elements is special for whatever reason. Most often this results in conditional statements to distinguish between ordinary elements and the primitive Null value.

This object just eats the lack of attribute error, and you can avoid checking for their existence.

It's nothing more than

class Null(object):

    def __init__(self, *args, **kwargs):
        "Ignore parameters."
        return None

    def __call__(self, *args, **kwargs):
        "Ignore method calls."
        return self

    def __getattr__(self, mname):
        "Ignore attribute requests."
        return self

    def __setattr__(self, name, value):
        "Ignore attribute setting."
        return self

    def __delattr__(self, name):
        "Ignore deleting attributes."
        return self

    def __repr__(self):
        "Return a string representation."
        return "<Null>"

    def __str__(self):
        "Convert to a string and return it."
        return "Null"

With this, if you do Null("any", "params", "you", "want").attribute_that_doesnt_exists() it won't explode, but just silently become the equivalent of pass.

Normally you'd do something like

if obj.attr:
    obj.attr()

With this, you just do:

obj.attr()

and forget about it. Beware that extensive use of the Null object can potentially hide bugs in your code.


回答 5

您可能还希望阅读本文(选择.pdf文件),该文章讨论了动态面向对象语言(例如Python)的设计模式。引用页面:

本文探讨了当使用动态的,高阶的,面向对象的编程语言解决相似的问题时,“四人帮”或“ GOF”一书中的模式是如何出现的。有些模式消失了-也就是说,语言功能直接支持它们,有些模式更简单或具有不同的关注点,而有些则基本上没有变化。

You may also wish to read this article (select the .pdf file), which discusses Design Patterns in dynamic object oriented languages (i.e. Python). To quote the page:

This paper explores how the patterns from the "Gang of Four", or "GOF" book, as it is often called, appear when similar problems are addressed using a dynamic, higher-order, object-oriented programming language. Some of the patterns disappear -- that is, they are supported directly by language features, some patterns are simpler or have a different focus, and some are essentially unchanged.


本文由 Python 实用宝典 作者:Python实用宝典 发表,其版权均为 Python 实用宝典 所有,文章内容系作者个人观点,不代表 Python 实用宝典 对观点赞同或支持。如需转载,请注明文章来源。
2

抱歉,评论已关闭!