this post was submitted on 26 Feb 2025
24 points (100.0% liked)

Python

6723 readers
1 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
 
class Node:
    def __init__(self, edges = set()):
        self.edges = edges


def main():
    foo = Node()
    bar = Node()
    quz = Node()

    foo.edges.add(bar)
    bar.edges.add(foo)

    assert(foo is not bar) # assertion succeeds
    assert(foo is not quz) # assertion succeeds
    assert(bar is not quz) # assertion succeeds
    assert(len(quz.edges) == 0) # assertion fails??


main()

spoilerMutable default values are shared across objects. The set in this case.

top 12 comments
sorted by: hot top controversial new old
[–] technohacker 10 points 5 days ago* (last edited 5 days ago) (1 children)

Oh I had a similar bug but with defaulted dicts. Default args are constructed once and reused. Not a problem for immutable args, but mutables like dicts (and sets I'd also assume) are all shared.

EDIT: whoops, didn't see you spoilered the answer, my bad! If it helps, i found my bug when dealing with cross-thread stuff, so that was a fun moment to bisect

[–] [email protected] 4 points 5 days ago (1 children)

You may like collections.defaultdict. Pass the constructor a factory function to be run when a key is missing.

dd = defaultdict(list)
dd['key'].append("value")
print(dd['key'])  # ["value"]
[–] technohacker 1 points 5 days ago

Ah sorry I meant a default argument which was a dict, thanks for the tip tho!

[–] Michal 2 points 4 days ago

Static set argument?

[–] [email protected] 3 points 5 days ago* (last edited 5 days ago)

Yeah, I discovered this when a coworker wrote code like def foo(timestamp = now()) and had fun debugging why there were a bunch of duplicate timestamps.

PEP 671 would add new syntax to ease the pain, but it's not accepted yet. It would allow for writing function definitions like one of these:

def bisect_right(a, x, lo=0, hi=>len(a), *, key=None):
def connect(timeout=>default_timeout):
def add_item(item, target=>[]):
def format_time(fmt, time_t=>time.time()):
[–] FizzyOrange 2 points 5 days ago (1 children)

Yeah Pylint catches this. If you aren't using Pylint you are writing bad Python.

[–] [email protected] 6 points 5 days ago (1 children)

That's a funny way to spell Ruff

[–] FizzyOrange 3 points 5 days ago (2 children)

Yeah I tried Ruff about a year ago and it only had really trivial lints so it wasn't a good replacement for Pylint. Is it on par yet?

[–] NostraDavid 2 points 4 days ago

It has implemented about half - I would check which the important ones are, and check if it's been implemented (which is being tracked in issue 970):

https://github.com/astral-sh/ruff/issues/970

Astral claims Ruff has more rules total (about 800), but implemented about half of Pylint (which has 400 rules, so 200 implemented).

[–] [email protected] 2 points 4 days ago* (last edited 4 days ago) (1 children)

What do you mean by trivial? I am not necessarily the most experienced coder, but it does a great job yelling at me to keep methods short and simple.

I'd suggest taking five minutes whenever and look up the ruff ruleset to see if it would be helpful for you.

Also maybe because I don't know how to use pylint in vs code, but the only semi useful thing it catches for me is if my venv doesn't have a library the code imports.

Edit: For example, Ruff has caught this bug (mutable argument defaults) in my code before.

[–] FizzyOrange 1 points 4 days ago

it does a great job yelling at me to keep methods short and simple

Yes style things like that are what I would consider trivial. I also think those are actively bad lints. Yes methods should be short in general, but making it a hard enforced limit means you end up getting sidetracked by refactoring when you only wanted to add one line to a method.

[–] [email protected] 1 points 4 days ago* (last edited 4 days ago)

Wow, I learned this bug during a job interview I had this week. I'm not much of a Python guy, but I was givin a Python coding challenge and I tried default initializing a parameter to an empty list. The guy interviewing me looked horrified and explained the problem and sent me an article about it to read later. It's a odd coincidence coming across it twice in one week.