python函数的闭合:实现给定的单词输出复数形式

需求:给定的单词,然后输出其复数形式(特殊情况除外)?

初级demo1:非封装函数法,一个函数解决所有。

import  re

def plural(noun):
    if re.search('[sxz]$', noun):
        return re.sub('$', 'es', noun)

    elif re.search('[^aeioudgkprt]h$', noun):
        return re.sub('$', 'es', noun)

    elif re.search('[^aeiou]y$', noun):
        return re.sub('y$', 'ies', noun)

    else:
        return noun + 's'
#实现函数调用,求单次复数形式

print(plural('zoo'))
print(plural("kitty"))
print(plural('coach'))
'''结果如下:
zoos
kitties
coaches
'''

进阶demo2:将功能全部抽象,剥离成函数

import re

#每条匹配规则都有自己的函数,它们返回对re.search() 函数调用结果。

#实现对sxz结尾单词的匹配和复数输出函数
def match_sxz(noun):
    return re.search('[sxz]$', noun)
def apply_sxz(noun):
    return re.sub('$', 'es', noun)

#实现对*h结尾单词的匹配和复数输出函数
def match_h(noun):
    return re.search('[^aeioudgkprt]h$', noun)
def apply_h(noun):
    return re.sub('$', 'es', noun)

#实现对*y结尾单词的匹配和复数输出函数
def match_y(noun):
    return re.search('[^aeiou]y$', noun)
def apply_y(noun):
    return re.sub('y$', 'ies', noun)

#实现对上面三种情况之外的单词输出复数
def match_default(noun):
    return True
def apply_default(noun):
    return noun + 's'

#一个函数对的序列,而不是一个函数(plural())实现多个条
rules = ((match_sxz, apply_sxz),
        (match_h, apply_h),
        (match_y, apply_y),
        (match_default, apply_default)
                                    )

def plural(noun):
    for matches_rule, apply_rule in rules:
        if matches_rule(noun):
            return apply_rule(noun)


#实现函数调用,求单次复数形式

print(plural('zoo'))
print(plural("kitty"))
print(plural('coach'))

'''结果如下:
zoos
kitties
coaches
'''

分析:该技术能够成功运作的原因是 Python 中一切都是对象,包括了函数。数据结构 rules 包含了函数——不是函数的名称,而是实际的函数对象。在 for 循环中被赋值后, matches_rule 和apply_rule 是可实际调用的函数。在第一次 for 循环的迭代过程中,这相当于调用 matches_sxz(noun),如果返回一个匹配值,将调用 apply_sxz(noun) 。

高阶demo3:闭合函数

import re

#在动态函数中使用外部参数值的技术称为 闭合【closures】
def build_match_and_apply_functions(pattern, search,replace):

    def matches_rule(word):
        return re.search(pattern, word)

    def apply_rule(word):
        return re.sub(search, replace, word)

    return (matches_rule, apply_rule)

#英语单词常见的4种复数变形方式
patterns = \
  (
    ('[sxz]$', '$', 'es'),
    ('[^aeioudgkprt]h$', '$', 'es'),
    ('(qu|[^aeiou])y$', 'y$', 'ies'),
    ('$', '$', 's')
  )

rules = [build_match_and_apply_functions(pattern, search,replace)
    for (pattern, search, replace) in patterns]
'''
它以 patterns 中的字符串序列为参数,并将其转换为一个函数序列。怎么做到的?通过将字符串“映
射”到 build_match_and_apply_functions() 函数。也就是说,它接受每组三重字符串为参数,并将该三个字符串作为实参调用
 build_match_and_apply_functions() 函数build_match_and_apply_functions() 函数返回一个包含两个函数的元组。也就是说
该规则 最后的结尾与前例在功能上是等价的:一个元组列表,每个元组都是一对函数。第一个函数是调用 re.search() 的匹配函数;
而第二个函数调用 re.sub() 的应用函数
'''

def plural(noun):
    for matches_rule, apply_rule in rules:
        if matches_rule(noun):
            return apply_rule(noun)

#实现函数调用,求单次复数形式

print(plural('zoo'))
print(plural("kitty"))
print(plural('coach'))

'''结果如下:
zoos
kitties
coaches
'''

统一声明:关于原创博客内容,可能会有部分内容参考自互联网,如有原创链接会声明引用;如找不到原创链接,在此声明如有侵权请联系删除哈。关于转载博客,如有原创链接会声明;如找不到原创链接,在此声明如有侵权请联系删除哈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涤生大数据

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值