Home Backend Development Python Tutorial 详细解析Python中__init__()方法的高级应用

详细解析Python中__init__()方法的高级应用

Jun 06, 2016 am 11:16 AM
python

通过工厂函数对 __init__() 加以利用

我们可以通过工厂函数来构建一副完整的扑克牌。这会比枚举所有52张扑克牌要好得多,在Python中,我们有如下两种常见的工厂方法:

  •     定义一个函数,该函数会创建所需类的对象。
  •     定义一个类,该类有创建对象的方法。这是一个完整的工厂设计模式,正如设计模式书所描述的那样。在诸如Java这样的语言中,工厂类层次结构是必须的,因为该语言不支持独立的函数。
  • 在Python中,类并不是必须的。只是当有相关的工厂非常复杂的时候才会显现出优势。Python的优势就是当一个简单的函数可以做的更好的时候我们决不强迫使用类层次结构。
  •     虽然这是一本关于面向对象编程的书,但函数真是一个好东西。这在Python中是常见的也是最地道的。

如果需要的话,我们总是可以将一个函数重写为适当的可调用对象。我们可以将一个可调用对象重构到我们的工厂类层次结构中。我们将在第五章《使用可调用对象和上下文》中学习可调用对象。

一般,类定义的优点是通过继承实现代码重用。工厂类的函数就是包装一些目标类层次结构和复杂对象的构造。如果我们有一个工厂类,当扩展目标类层次结构的时候,我们可以添加子类到工厂类中。这给我们提供了多态性工厂类;不同的工厂类定义具有相同的方法签名,可以交替使用。

这类水平的多态性对于静态编译语言如Java或C++非常有用。编译器可以解决类和方法生成代码的细节。

如果选择的工厂定义不能重用任何代码,则在Python中类层次结构不会有任何帮助。我们可以简单的使用具有相同签名的函数。

以下是我们各种Card子类的工厂函数:

def card(rank, suit):
  if rank == 1:
    return AceCard('A', suit)
  elif 2 <= rank < 11: 
    return NumberCard(str(rank), suit)

  elif 11 <= rank < 14:
    name = {11: 'J', 12: 'Q', 13: 'K' }[rank]
    return FaceCard(name, suit)
  else:
    raise Exception("Rank out of range")

Copy after login

这个函数通过数值类型的rank和suit对象构建Card类。我们现在可以非常简单的构建牌了。我们已经封装构造问题到一个单一的工厂函数中,允许应用程序在不知道精确的类层次结构和多态设计是如何工作的情况下进行构建。

下面是一个如何通过这个工厂函数构建一副牌的示例:

deck = [card(rank, suit) for rank in range(1,14) for suit in (Club, Diamond, Heart, Spade)]

Copy after login

它枚举了所有的牌值和花色来创建完整的52张牌。
1. 错误的工厂设计和模糊的else子句

注意card()函数里面的if语句结构。我们没有使用“包罗万象”的else子句来做任何处理;我们只是抛出异常。使用“包罗万象”的else子句会引出一个小小的辩论。

一方面,从属于else子句的条件不能不言而喻,因为它可能隐藏着微妙的设计错误。另一方面,一些else子句确实是显而易见的。

重要的是要避免含糊的else子句。

考虑下面工厂函数定义的变体:

def card2(rank, suit):
  if rank == 1: 
    return AceCard('A', suit)
  elif 2 <= rank < 11: 
    return NumberCard(str(rank), suit)
  else:
    name = {11: 'J', 12: 'Q', 13: 'K'}[rank]
    return FaceCard(name, suit)

Copy after login

以下是当我们尝试创建整副牌将会发生的事情:

deck2 = [card2(rank, suit) for rank in range(13) for suit in (Club, Diamond, Heart, Spade)]

Copy after login

它起作用了吗?如果if条件更复杂了呢?

一些程序员扫视的时候可以理解这个if语句。其他人将难以确定是否所有情况都正确执行了。

对于高级Python编程,我们不应该把它留给读者去演绎条件是否适用于else子句。对于菜鸟条件应该是显而易见的,至少也应该是显示的。

何时使用“包罗万象”的else

尽量的少使用。使用它只有当条件是显而易见的时候。当有疑问时,显式的并抛出异常。

避免含糊的else子句。
2. 简单一致的使用elif序列

我们的工厂函数card()是两种常见工厂设计模式的混合物:

  • if-elif序列
  • 映射

为了简单起见,最好是专注于这些技术的一个而不是两个。

我们总是可以用映射来代替elif条件。(是的,总是。但相反是不正确的;改变elif条件为映射将是具有挑战性的。)

以下是没有映射的Card工厂:

def card3(rank, suit):
  if rank == 1: 
    return AceCard('A', suit)
  elif 2 <= rank < 11: 
    return NumberCard(str(rank), suit)
  elif rank == 11:
    return FaceCard('J', suit)
  elif rank == 12:
    return FaceCard('Q', suit)
  elif rank == 13:
    return FaceCard('K', suit)
  else:
    raise Exception("Rank out of range")

Copy after login

我们重写了card()工厂函数。映射已经转化为额外的elif子句。这个函数有个优点就是它比之前的版本更加一致。
3. 简单的使用映射和类对象

在一些示例中,我们可以使用映射来代替一连串的elif条件。很可能发现条件太复杂,这个时候或许只有使用一连串的elif条件来表达才是明智的选择。对于简单示例,无论如何,映射可以做的更好且可读性更强。

因为class是最好的对象,我们可以很容易的映射rank参数到已经构造好的类中。

以下是仅使用映射的Card工厂:

 def card4(rank, suit):
  class_= {1: AceCard, 11: FaceCard, 12: FaceCard, 13: FaceCard}.get(rank, NumberCard)
  return class_(rank, suit)

Copy after login

我们已经映射rank对象到类中。然后,我们传递rank值和suit值到类来创建最终的Card实例。

最好我们使用defaultdict类。无论如何,对于微不足道的静态映射不会比这更简单了。看起来像下面代码片段那样:

defaultdict(lambda: NumberCard, {1: AceCard, 11: FaceCard, 12: FaceCard, 12: FaceCard})

注意:defaultdict类默认必须是零参数的函数。我们已经使用了lambda创建必要的函数来封装常量。这个函数,无论如何,都有一些缺陷。对于我们之前版本中缺少1到A和13到K的转换。当我们试图增加这些特性时,一定会出现问题的。

我们需要修改映射来提供可以和字符串版本的rank对象一样的Card子类。对于这两部分的映射我们还可以做什么?有四种常见解决方案:

  1. 可以做两个并行的映射。我们不建议这样,但是会强调展示不可取的地方。
  2. 可以映射个二元组。这个同样也会有一些缺点。
  3. 可以映射到partial()函数。partial()函数是functools模块的一个特性。
  4. 可以考虑修改我们的类定义,这种映射更容易。可以在下一节将__init__()置入子类定义中看到。

我们来看看每一个具体的例子。
3.1. 两个并行映射

以下是两个并行映射解决方案的关键代码:

class_= {1: AceCard, 11: FaceCard, 12: FaceCard, 13: FaceCard}.get(rank, NumberCard)
rank_str= {1:'A', 11:'J', 12:'Q', 13:'K'}.get(rank, str(rank))
return class_(rank_str, suit)

Copy after login

这并不可取的。它涉及到重复映射键1、11、12和13序列。重复是糟糕的,因为在软件更新后并行结构依然保持这种方式。

不要使用并行结构

并行结构必须使用元组或一些其他合适的集合来替代。
3.2. 映射到元组的值

以下是二元组映射的关键代码:

class_, rank_str= {
  1: (AceCard,'A'),
  11: (FaceCard,'J'),
  12: (FaceCard,'Q'),
  13: (FaceCard,'K'),
}.get(rank, (NumberCard, str(rank)))
return class_(rank_str, suit)

Copy after login

这是相当不错的。不需要过多的代码来分类打牌中的特殊情况。当我们需要改变Card类层次结构来添加额外的Card子类时,我们将看到它如何被修改或被扩展。

将rank值映射到一个类对象的确让人感觉奇怪,且只有类初始化所需两个参数中的其中之一。将牌值映射到一个简单的类或没有提供一些混乱参数(但不是所有)的函数对象似乎会更合理。
3.3. partial函数解决方案

相比映射到二元组函数和参数之一,我们可以创建一个partial()函数。这是一个已经提供一些(但不是所有)参数的函数。我们将从functools库中使用partial()函数来创建一个带有rank参数的partial类。

以下是一个映射rank到partial()函数,可用于对象创建:

from functools import partial
part_class= {
  1: partial(AceCard, 'A'),
  11: partial(FaceCard, 'J'),
  12: partial(FaceCard, 'Q'),
  13: partial(FaceCard, 'K'),
}.get(rank, partial(NumberCard, str(rank)))
return part_class(suit)

Copy after login

映射将rank对象与partial()函数联系在一起,并分配给part_class。这个partial()函数可以被应用到suit对象来创建最终的对象。partial()函数是一种常见的函数式编程技术。它在我们有一个函数来替代对象方法这一特定的情况下使用。

不过总体而言,partial()函数对于大多数面向对象编程并没有什么帮助。相比创建partial()函数,我们可以简单地更新类的方法来接受不同组合的参数。partial()函数类似于给对象构造创建一个连贯的接口。
3.4. 连贯的工厂类接口

在某些情况下,我们设计的类为方法的使用定义了顺序,衡量方法的顺序很像partial()函数。

在一个对象表示法中我们可能会有x.a() .b()。我们可以把它当成x(a, b)。x.a()函数是等待b()的一类partial()函数。我们可以认为它就像x(a)(b)那样。

这里的想法是,Python给我们提供两种选择来管理状态。我们既可以更新对象又可以创建有状态性的(在某种程度上)partial()函数。由于这种等价,我们可以重写partial()函数到一个连贯的工厂对象中。我们使得rank对象的设置为一个连贯的方法来返回self。设置suit对象将真实的创建Card实例。

以下是一个连贯的Card工厂类,有两个方法函数,必须在特定顺序中使用:

class CardFactory:
  def rank(self, rank):
    self.class_, self.rank_str= {
        1: (AceCard, 'A'),
        11: (FaceCard,'J'),
        12: (FaceCard,'Q'),
        13: (FaceCard,'K'),
    }.get(rank, (NumberCard, str(rank)))
    return self
  def suit(self, suit):
    return self.class_(self.rank_str, suit)

Copy after login

rank()方法更新构造函数的状态,suit()方法真实的创建了最终的Card对象。

这个工厂类可以像下面这样使用:

card8 = CardFactory()
deck8 = [card8.rank(r+1).suit(s) for r in range(13) for s in (Club, Diamond, Heart, Spade)]

Copy after login

首先,我们创建一个工厂实例,然后我们使用那个实例创建Card实例。这并没有实质性改变__init__()本身在Card类层次结构中如何运作的。然而,它确实改变了我们客户端应用程序创建对象的方式。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use mysql after installation How to use mysql after installation Apr 08, 2025 am 11:48 AM

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

How does PS feathering control the softness of the transition? How does PS feathering control the softness of the transition? Apr 06, 2025 pm 07:33 PM

The key to feather control is to understand its gradual nature. PS itself does not provide the option to directly control the gradient curve, but you can flexibly adjust the radius and gradient softness by multiple feathering, matching masks, and fine selections to achieve a natural transition effect.

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

How to set up PS feathering? How to set up PS feathering? Apr 06, 2025 pm 07:36 PM

PS feathering is an image edge blur effect, which is achieved by weighted average of pixels in the edge area. Setting the feather radius can control the degree of blur, and the larger the value, the more blurred it is. Flexible adjustment of the radius can optimize the effect according to images and needs. For example, using a smaller radius to maintain details when processing character photos, and using a larger radius to create a hazy feeling when processing art works. However, it should be noted that too large the radius can easily lose edge details, and too small the effect will not be obvious. The feathering effect is affected by the image resolution and needs to be adjusted according to image understanding and effect grasp.

What should I do if the PS card is in the loading interface? What should I do if the PS card is in the loading interface? Apr 06, 2025 pm 06:54 PM

The loading interface of PS card may be caused by the software itself (file corruption or plug-in conflict), system environment (due driver or system files corruption), or hardware (hard disk corruption or memory stick failure). First check whether the computer resources are sufficient, close the background program and release memory and CPU resources. Fix PS installation or check for compatibility issues for plug-ins. Update or fallback to the PS version. Check the graphics card driver and update it, and run the system file check. If you troubleshoot the above problems, you can try hard disk detection and memory testing.

How to optimize database performance after mysql installation How to optimize database performance after mysql installation Apr 08, 2025 am 11:36 AM

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.

What impact does PS feathering have on image quality? What impact does PS feathering have on image quality? Apr 06, 2025 pm 07:21 PM

PS feathering can lead to loss of image details, reduced color saturation and increased noise. To reduce the impact, it is recommended to use a smaller feather radius, copy the layer and then feather, and carefully compare the image quality before and after feathering. In addition, feathering is not suitable for all cases, and sometimes tools such as masks are more suitable for handling image edges.

How to optimize MySQL performance for high-load applications? How to optimize MySQL performance for high-load applications? Apr 08, 2025 pm 06:03 PM

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

See all articles