- Functional Python Programming
- Steven F. Lott
- 382字
- 2021-08-27 19:20:24
Using strings
Since Python strings are immutable, they're an excellent example of functional programming objects. A Python str object has a number of methods, all of which produce a new string as the result. These methods are pure functions with no side effects.
The syntax for str method functions is postfix, where most functions are prefix. This means that complex string operations can be hard to read when they're co-mingled with conventional functions. For example, in this expression, len(variable.title()), the title() method is in postfix notation and the len() function is in prefix notation.
When scraping data from a web page, we may have a function to clean the data. This could apply a number of transformations to a string to clean up the punctuation and return a Decimal object for use by the rest of the application. This will involve a mixture of prefix and postfix syntax.
It could look like the following command snippet:
from decimal import *
from typing import Text, Optional def clean_decimal(text: Text) -> Optional[Text]: if text is None: return None return Decimal(
text.replace("$", "").replace(",", ""))
This function does two replacements on the string to remove $ and , string values. The resulting string is used as an argument to the Decimal class constructor, which returns the desired object. If the input value is None, this is preserved; this is why the Optional type hint is used.
To make the syntax look more consistent, we can consider defining our own prefix functions for the string method functions, as follows:
def replace(str: Text, a: Text, b: Text) -> Text:
return str.replace(a,b)
This can allow us to use Decimal(replace(replace(text, "$", ""), ",", "")) with consistent-looking prefix syntax. It's not clear whether this kind of consistency is a significant improvement over the mixed prefix and postfix notation. This may be an example of a foolish consistency.
A slightly better approach may be to define a more meaningful prefix function to strip punctuation, such as the following command snippet:
def remove(str: Text, chars: Text) -> Text:
if chars:
return remove(
str.replace(chars[0], ""),
chars[1:]
) return str
This function will recursively remove each of the characters from the chars variable. We can use it as Decimal(remove(text, "$,")) to make the intent of our string cleanup more clear.
- 控糖控脂健康餐
- C/C++算法從菜鳥(niǎo)到達(dá)人
- Spring Boot+Spring Cloud+Vue+Element項(xiàng)目實(shí)戰(zhàn):手把手教你開(kāi)發(fā)權(quán)限管理系統(tǒng)
- Python測(cè)試開(kāi)發(fā)入門(mén)與實(shí)踐
- 算法精粹:經(jīng)典計(jì)算機(jī)科學(xué)問(wèn)題的Python實(shí)現(xiàn)
- Mastering Scientific Computing with R
- Python之光:Python編程入門(mén)與實(shí)戰(zhàn)
- Mastering Xamarin.Forms(Second Edition)
- Swift 4 Protocol-Oriented Programming(Third Edition)
- AutoCAD 2009實(shí)訓(xùn)指導(dǎo)
- R語(yǔ)言數(shù)據(jù)可視化:科技圖表繪制
- Maker基地嘉年華:玩轉(zhuǎn)樂(lè)動(dòng)魔盒學(xué)Scratch
- PyQt編程快速上手
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)(第二版)
- Microsoft Exchange Server 2016 PowerShell Cookbook(Fourth Edition)