diff --git a/pep-0505.txt b/pep-0505.txt index eb5545874..8397520e5 100644 --- a/pep-0505.txt +++ b/pep-0505.txt @@ -466,25 +466,25 @@ improved. This code *might* be improved, though, if there was a syntactic shortcut for this common need to supply a default value. We'll assume the fictitious -operator ``✊🍆`` to avoid a premature debate about the spelling of said +operator ``🔑`` to avoid a premature debate about the spelling of said operator:: class BaseUploadObject(object): def find_ctype(self, filename): ctype, encoding = mimetypes.guess_type(filename) - return ctype ✊🍆 'application/octet-stream' + return ctype 🔑 'application/octet-stream' class UploadContent(BaseUploadObject): def __init__(self, content, filename=None, content_type=None): self.content = content - self.filename = filename ✊🍆 self.get_random_filename() - self.content_type = content_type ✊🍆 self.find_ctype(self.filename) + self.filename = filename 🔑 self.get_random_filename() + self.content_type = content_type 🔑 self.find_ctype(self.filename) class UploadFile(BaseUploadObject): def __init__(self, path, filename=None, content_type=None): self.path = path - self.filename = filename ✊🍆 os.path.split(path)[1] - self.content_type = content_type ✊🍆 self.find_ctype(self.filename) + self.filename = filename 🔑 os.path.split(path)[1] + self.content_type = content_type 🔑 self.find_ctype(self.filename) This syntax has an intuitive ordering of the operands, e.g. ``ctype`` -- the preferred value -- comes before the fallback value. The terseness of the syntax @@ -737,20 +737,20 @@ in the following way. Some simple examples:: - >>> 1 ✊🍆 2 + >>> 1 🔑 2 1 - >>> None ✊🍆 2 + >>> None 🔑 2 2 - >>> 1 ✊🍆 None + >>> 1 🔑 None 1 Importantly, note that the right operand is not evaluated unless the left operand is None:: >>> def err(): raise Exception('foo') - >>> 1 ✊🍆 err() + >>> 1 🔑 err() 1 - >>> None ✊🍆 err() + >>> None 🔑 err() Traceback (most recent call last): File "", line 1, in File "", line 1, in err @@ -762,15 +762,15 @@ this makes the operator easy to chain:: >>> timeout = None >>> local_timeout = 60 >>> global_timeout = 300 - >>> timeout ✊🍆 local_timeout ✊🍆 global_timeout + >>> timeout 🔑 local_timeout 🔑 global_timeout 60 >>> local_timeout = None - >>> timeout ✊🍆 local_timeout ✊🍆 global_timeout + >>> timeout 🔑 local_timeout 🔑 global_timeout 300 >>> import time - >>> timeout ✊🍆 local_timeout ✊🍆 global_timeout ✊🍆 time.sleep(10) + >>> timeout 🔑 local_timeout 🔑 global_timeout 🔑 time.sleep(10) 300 Note in the last example that ``time.sleep(10)`` represents an expensive @@ -784,29 +784,29 @@ read and write:: >>> user_flag = None >>> default_flag = True - >>> not user_flag ✊🍆 default_flag # Same as next expression. + >>> not user_flag 🔑 default_flag # Same as next expression. False - >>> not (user_flag ✊🍆 default_flag) # Same as previous. + >>> not (user_flag 🔑 default_flag) # Same as previous. False - >>> (not user_flag) ✊🍆 default_flag # Different from previous. + >>> (not user_flag) 🔑 default_flag # Different from previous. True >>> user_quantity = None >>> default_quantity = 1 - >>> 1 == user_quantity ✊🍆 default_quantity # Same as next expression. + >>> 1 == user_quantity 🔑 default_quantity # Same as next expression. True - >>> 1 == (user_quantity ✊🍆 default_quantity) # Same as previous. + >>> 1 == (user_quantity 🔑 default_quantity) # Same as previous. True - >>> (1 == user_quantity) ✊🍆 default_quantity # Different from previous. + >>> (1 == user_quantity) 🔑 default_quantity # Different from previous. False >>> user_words = None >>> default_words = ['foo', 'bar'] - >>> 'foo' in user_words ✊🍆 default_words # Same as next expression. + >>> 'foo' in user_words 🔑 default_words # Same as next expression. True - >>> 'foo' in (user_words ✊🍆 default_words) # Same as previous. + >>> 'foo' in (user_words 🔑 default_words) # Same as previous. True - >>> ('foo' in user_words) ✊🍆 default_words # Different from previous. + >>> ('foo' in user_words) 🔑 default_words # Different from previous. Traceback (most recent call last): File "", line 1, in TypeError: argument of type 'NoneType' is not iterable @@ -814,7 +814,7 @@ read and write:: >>> user_discount = None >>> default_discount = 0.9 >>> price = 100 - >>> price * user_discount ✊🍆 default_discount + >>> price * user_discount 🔑 default_discount Recall the example above of calculating the cost of items in a shopping cart, and the easy-to-miss bug. This type of bug is not possible with the ``None``- @@ -823,25 +823,25 @@ coalescing operator, because there is no implicit type coersion to ``bool``:: >>> price = 100 >>> requested_quantity = 0 >>> default_quantity = 1 - >>> (requested_quantity ✊🍆 default_quantity) * price + >>> (requested_quantity 🔑 default_quantity) * price 0 The ``None``-coalescing operator also has a corresponding assignment shortcut. The following assignments are semantically equivalent:: - >>> foo ✊🍆= [] - >>> foo = foo ✊🍆 [] + >>> foo 🔑= [] + >>> foo = foo 🔑 [] The ``None`` coalescing operator improves readability, especially when handling default function arguments. Consider again the example of requests, rewritten to use ``None``-coalescing:: def __init__(self, data=None, files=None, headers=None, params=None, hooks=None): - self.data = data ✊🍆 [] - self.files = files ✊🍆 [] - self.headers = headers ✊🍆 {} - self.params = params ✊🍆 {} - self.hooks = hooks ✊🍆 {} + self.data = data 🔑 [] + self.files = files 🔑 [] + self.headers = headers 🔑 {} + self.params = params 🔑 {} + self.hooks = hooks 🔑 {} The operator makes the intent easier to follow (by putting operands in an intuitive order) and is more concise than the ternary operator, while still @@ -1087,7 +1087,7 @@ Member Access Operator", etc. The coalescing operator would invoke this dunder method. The following two expressions are semantically equivalent:: - >>> foo ✊🍆 bar + >>> foo 🔑 bar >>> bar if foo.__coalesce__() else foo The coalesced attribute and index access operators would invoke the same dunder @@ -1101,7 +1101,7 @@ just like ``None``. For example, the ``pyasn1`` package has a type called ``Null`` that represents an ASN.1 ``null``. >>> from pyasn1.type import univ - >>> univ.Null() ✊🍆 univ.Integer(123) + >>> univ.Null() 🔑 univ.Integer(123) Integer(123) In addition to making the proposed operators less specialized, this