惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Full Disclosure
WordPress大学
WordPress大学
小众软件
小众软件
Cloudbric
Cloudbric
AWS News Blog
AWS News Blog
腾讯CDC
量子位
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Vulnerabilities – Threatpost
Scott Helme
Scott Helme
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
IT之家
IT之家
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
Google Online Security Blog
Google Online Security Blog
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
罗磊的独立博客
L
LINUX DO - 最新话题
博客园 - Franky
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
The Last Watchdog
The Last Watchdog
J
Java Code Geeks
AI
AI
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
Help Net Security
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
I
Intezer
S
Securelist

Martin Heinz's Blog

A Guide to Python's Weak References Using weakref Module Recent Docker BuildKit Features You're Missing Out On Modern Git Commands and Features You Should Be Using Everything You Can Do with Python's textwrap Module Monitoring Indoor Air Quality with Prometheus, Grafana and a CO2 Sensor Everything You Can Do with Python's bisect Module You Don't Need a Dedicated Cache Service - PostgreSQL as a Cache A Collection of Docker Images To Solve All Your Debugging Needs Lessons Learned From Writing 100 Articles Debugging Crashes and Deadlocks in Python using PyStack Goodbye etcd, Hello PostgreSQL: Running Kubernetes with an SQL Database Remote Interactive Debugging of Python Applications Running in Kubernetes The Right Way to Run Shell Commands From Python Real Multithreading is Coming to Python - Learn How You Can Use It Now Python's Missing Batteries: Essential Libraries You're Missing Out On Kubernetes-Native Synthetic Monitoring with Kuberhealthy Make Your CLI Demos a Breeze with Zero Stress and Zero Mistakes Reduce - The Power of a Single Python Function Why I Will Never Use Alpine Linux Ever Again Cgroups - Deep Dive into Resource Management in Kubernetes Dictionary Dispatch Pattern in Python Boost Your Python Application Performance using Continuous Profiling Lazy Evaluation Using Recursive Python Generators Python Magic Methods You Haven't Heard About Getting Started with Mastodon API in Python Backup-and-Restore of Containers with Kubernetes Checkpointing API Getting Started with Google APIs in Python Python CLI Tricks That Don't Require Any Code Whatsoever All The Ways To Introspect Python Objects at Runtime What is Python's "self" Argument, Anyway? Python List Comprehensions Are More Powerful Than You Might Think You Should Be Using Python's Walrus Operator - Here's Why Recipes and Tricks for Effective Structural Pattern Matching in Python It's Time to Say Goodbye to These Obsolete Python Libraries Advanced Features of Kubernetes' Horizontal Pod Autoscaler Data and System Visualization Tools That Will Boost Your Productivity Stop Messing with Kubernetes Finalizers Automate All the Boring Kubernetes Operations with Python End-to-End Monitoring with Grafana Cloud with Minimal Effort Bitly | bit.ly/3JLmSgA Bitly | bit.ly/3uETfbi Ultimate CI Pipeline for All of Your Python Projects Bitly | bit.ly/3M30D82 Bitly | bit.ly/3oMJ6qR Bitly | bit.ly/3IRD7IK Bitly | bit.ly/3A3B69t Profiling and Analyzing Performance of Python Programs Bitly | bit.ly/30uviIM Bitly | bit.ly/3E1X2mw Bitly | bit.ly/3Dv7JxP Bitly | bit.ly/3GG1BEz Bitly | bit.ly/3lLavs4 Bitly | bit.ly/39TqP3m Bitly | bit.ly/3A5Mpx8 Bitly | bit.ly/3kGwPl4 Bitly | bit.ly/3iHtulU Bitly | bit.ly/3xGjtKS Bitly | bit.ly/3h8DZg0 Bitly | bit.ly/2RQn1dG Bitly | bit.ly/3p2B5wW Bitly | bit.ly/3tULpb0 Bitly | bit.ly/2PHVudx Bitly | bit.ly/3uPtnb0 Bitly | bit.ly/3dg3QR9 Bitly | bit.ly/3qHtSkZ Bitly | bit.ly/3kIkTPr Bitly | bit.ly/3qlRAUN Bitly | bit.ly/3pCUJ26 Hardening Docker and Kubernetes with seccomp Bitly | bit.ly/34ZhIMt Bitly | bit.ly/3qSO7h0 Bitly | bit.ly/3muGLOk Bitly | bit.ly/35xN79v Bitly | bit.ly/3mLGshK Bitly | bit.ly/2IvkGQl Bitly | bit.ly/2Sk1KFK Bitly | bit.ly/3iCNIL6 Bitly | bit.ly/3beQPpy Saving Your Linux Machine from Certain Death New Features in Python 3.9 You Should Know About Deploy Any Python Project to Kubernetes Analyzing Docker Image Security Recursive SQL Queries with PostgreSQL Automating Every Aspect of Your Python Project Tour of Python Itertools Implementing 2D Physics in Javascript Ultimate Setup for Your Next Python Project Making Python Programs Blazingly Fast Security and Cryptography Mistakes You Are Probably Doing All The Time Going Serverless with OpenFaaS and Golang - Building Optimized Templates Going Serverless with OpenFaaS and Golang - The Ultimate Setup and Workflow Setting Up Swagger Docs for Golang API Building RESTful APIs in Golang Pytest Features, That You Need in Your (Testing) Life Setting up GitHub Package Registry with Docker and Golang Ultimate Setup for Your Next Golang Project Python Tips and Trick, You Haven't Already Seen, Part 2. Tricks for Postgres and Docker that will make your life easier Getting The Most Out of Reading Books - Reading The "Professional Way" Python Tips and Trick, You Haven't Already Seen
Weird Python "Features" That Might Catch You By Surprise
Martin · 2023-08-15 · via Martin Heinz's Blog

From time to time, when coding, we all run into weird behaviours of the programming language. Sometimes it's a "feature" we weren't aware of, sometimes it's just quirky behaviour of the language, and sometimes it's borderline bug. Python - as any other programming language - has these eyebrows-raising quirks, so here's a list of weird Python "features" that might catch you off-guard.

"Features"

Let's start with some odd behaviours, which some might consider "features". Such as:


class A:
    def func(self):
        print("A")

class B:
    def func(self):
        print("B")

a = A()
a.func()  # A

a.__class__ = B
a.func()  # B

In the above example we assigned B class to a.__class__ attribute, which changes as functions to the ones in B class.

This works because __class__ is just an attribute on an instance. You can reassign it however you like. Therefore, you can change the type of object just by merely assigning a different class to its __class__ attribute.

Next up are for loops. But for loops are so simple and basic, what surprising feature could they have?


values = "abc"
some_dict = {"key": ""}
for some_dict["key"] in values:
    print(some_dict)

# {'key': 'a'}
# {'key': 'b'}
# {'key': 'c'}

Python interpreter doesn't really care about the variable you put in the first half of for loop statement, as long as it can assign values to it. In this case it simply assign the individual characters from the sequence (values) to the key in the dictionary.

While the above was surprising for me when I first saw it, it isn't that weird and is an actual feature, and there definitely are legitimate use-cases for it.

Tuples

We all (probably) know that tuples are immutable - you define them once and then can't change their contents - right?


some_tuple = ([1], [2])
some_tuple[0].append(2)  # Worked!
print(some_tuple)
# ([1, 2], [2])

Well, not quite. While you cannot modify the tuple itself, you can modify its elements if they're mutable, which lists are.

Important thing to understand is that tuples only hold references to the objects, in this case lists. Therefore, you cannot change the reference, e.g. replace/delete the list, but you absolutely can change its value.

Also, to add to the confusion about tuple immutability, you can also run the following code without error:


some_tuple = ([1], [2])
print(id(some_tuple))
# 139997458815936
some_tuple += ([3],)
print(id(some_tuple))  # identity changed, therefore it's a new object
# 139997458018880

tuple implements both + and += operators, but they don't modify the tuple in-place. Rather, they create a new tuple object and replace the original one. We can see that by checking identity of the variable using id function.

And finally, if the previous two examples didn't surprise you, then this one will surely raise some eyebrows. This snippet works, but actually doesn't, huh?


x = ([1, 2],)

try:
    x[0] += [3, 4]
except Exception as e:
    print(e)  # 'tuple' object does not support item assignment

# Traceback (most recent call last):
#   File "/home/martin/Projects/learning-notes/posts/Python Weirdness/examples.py", line 4, in <module>
#     x[0] += [3, 4]
# TypeError: 'tuple' object does not support item assignment

print(x)
# ([1, 2, 3, 4],)

In this snippet, we added [3, 4] to the first element of a tuple (x[0]) using in-place operator (+=) and we received TypeError. Yet when we look at the x variable afterwards, we can see that the 2 new elements ([3, 4]) were added anyway.

We already established that you can modify mutable elements in an immutable tuple. So what's happening here?


# Pseudo-code
class List:
    def __iadd__(self, other):
        self.extend(other)
        return self

x = ([1, 2],)
x[0].extend([3, 4]); x[0] = x[0]

The problem is += operator, which calls the __iadd__ magic method of list in the background. This method first uses the extend method to add the elements to the existing list and then returns the list itself - effectively executing x[0].extend([3, 4]); x[0] = x[0]. The extend succeeds, because list is mutable, but the assignment fails, because tuple is not. We only needed to perform extend, but that's not how += is implemented on list class. Sometimes implementation details matter.

Recursion

Next area full of surprises is recursion, and when you pair it with lambda expression - naturally - weird things will happen:


(lambda x : x(x))(lambda x : x(x))
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   File "<stdin>", line 1, in <lambda>
#   File "<stdin>", line 1, in <lambda>
#   File "<stdin>", line 1, in <lambda>
#   [Previous line repeated 996 more times]
# RecursionError: maximum recursion depth exceeded

I don't think that the fact that the above line of code causes stack overflow is really that surprising, it's more the fact that it's a valid piece of code in Python for whatever reason. I think there's no point trying to decipher it, because no sane person should write it.

A little less esoteric and possibly useful fact about recursion in Python is that you can create circular references:


a = [1, 2, 3]
a.append(a)
print(a)
# [1, 2, 3, [...]]

print(a[3])
# [1, 2, 3, [...]]

a = a[1:]
print(a)
# [2, 3, [1, 2, 3, [...]]]

Here we appended a list to itself, and Python even has nice way of representing it using [...]. If you try to access the self-referential element, you will - unsurprisingly - get the same thing. You can obviously perform any other operation on this list, such as slicing, which lands some interesting results as you can see above.

F-Strings

f-strings are great, they're very powerful and over the years they received a lot of useful features. To the point that you can do some weird things with them. Such as putting lambda expressions inside of them:


print(f"{(lambda x: x**2)(3)}")
# 9

And if you decide to pair f-strings with the recently introduced walrus operator (:=) then you can define variables inside f-string too:


from datetime import datetime

print(f"Today is: {(today:=datetime.today()):%Y-%m-%d}, which is {today:%A}")
# Today is: 2023-05-01, which is Monday

print(today)
# 2023-05-01 13:43:30.827182

And because the f-string doesn't have its own scope, the variable - today in the above example - can be used outside/beyond the f-string itself! Which makes sense I guess, but doesn't feel right or intuitive to me...

Borderline Bugs

One last Python "quirk", which I personally consider to be a bug, is a behavior of raw string literals. If you're not familiar with raw strings, then these are strings denoted/prefixed with r and they treat backslash as literal character rather than escape/special character.

Well, but what's the issue with these raw strings?


literal = r"some string\"
# SyntaxError: unterminated string literal (detected at line 1)

literal = r"some string\\"  # 2 backslashes
print(literal)  # some string\\

If you try to create a raw string ending with a backslash - r"...\" - you will receive a SyntaxError claiming that the string is not terminated. So clearly Python interpreter treats the backslash as an escape character for the closing quote sign, but that doesn't make sense, because it's raw string and backslash should be treated as literal character.

In fact this is common enough issue, that there is an entry for it in Python's design FAQ, as well as bug submission in a bug tracker.

Closing Thoughts

While you probably won't run into most of these oddities in your day-to-day coding, I think it's good to know about them and there's a lot we can learn from these weird language quirks. They force us to look a bit deeper and understand what's happening under the covers, which in turn makes us better Python developers.