Login
You're viewing the code4lib.social public feed.
  • Nov 24, 2026, 9:17 PM

    hey did you know you can create attributes on python objects which have dots in their names?

    that is, object foo can have an attribute of "bar.baz" ?

    that can't be accessed with foo.bar.baz, because that tries to access bar on foo, which doesn't exist?

    DON'T FUCKING DO THIS

    💬 12🔄 17⭐ 89

Replies

  • Nov 24, 2026, 9:17 PM

    >>> class Object:
    ... pass
    ...
    >>> obj=Object()
    >>> setattr(obj,'foo.bar','hi')
    >>> obj.foo.bar
    Traceback (most recent call last):
    File "<python-input-4>", line 1, in <module>
    obj.foo.bar
    ^^^^^^^
    AttributeError: 'Object' object has no attribute 'foo'

    💬 6🔄 1⭐ 17
  • 💬 3🔄 5⭐ 44
  • Nov 24, 2026, 9:19 PM

    @foone this reminds me of a friend who created a linux virtual network interface with a / in its name. Which you then never could get rid off, since it populated stuff in /sys/.../blah/2/ where blah/2 was the name of the interface. Had to reboot the whole thing.

    💬 0🔄 1⭐ 13
  • 💬 1🔄 0⭐ 4
  • 💬 0🔄 0⭐ 2
  • 💬 0🔄 0⭐ 1
  • 💬 0🔄 0⭐ 3
  • Nov 24, 2026, 9:35 PM

    @foone heh, I recently ran into an object storage ORM library that did exactly this. the root object had ".foo.bar" attribute keys for representing the nested objects and then magic functions to compute the lookup key and set/get, rather than properly constructing nested objects. they also injected callback functions and some other weird DI stuff. properly cursed.

    💬 2🔄 0⭐ 2
  • 💬 0🔄 0⭐ 0
  • Nov 24, 2026, 9:40 PM

    @gsuberland @foone this is the kind of thing I've done when I want a cache or sidecar object with the same lifetime and copy semantics.

    I've done munged __private, leading symbol, leading null, non-string, everything.

    💬 1🔄 0⭐ 1
  • Nov 24, 2026, 9:41 PM

    @gsuberland @foone (I've also got a weakref object identity dictionary I copy occasionally, which does something similar, but it forgets the extra data if the object gets copied.)

    💬 0🔄 0⭐ 1
  • 💬 0🔄 0⭐ 3
  • 💬 1🔄 0⭐ 0
  • Nov 24, 2026, 10:05 PM

    @foone you could probably also override __getattr__ and __setattr__ for Object and make this work the way...someone that wanted dots in their object attributes, or maybe more reasonably expected to be able to just lazily create a dict called "foo" with a key called "bar"..wanted it to work.

    💬 1🔄 0⭐ 0
  • Nov 24, 2026, 10:33 PM

    @old_angry_queer @foone I have a vague recollection that something like this, so dot notation could be used on dicts in a class broke debugging on a project I worked on a few years ago. Something like that.

    And we never fixed it.

    💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 1
  • Nov 25, 2026, 1:42 AM

    @foone When you prepare to use `setattr` you must first dig two memory holes...

    💬 0🔄 0⭐ 1
  • Nov 24, 2026, 9:21 PM

    @foone I did know this but it coming up in the context of the above thread is menacing as hell

    💬 0🔄 0⭐ 2
  • 💬 0🔄 0⭐ 1
  • 💬 2🔄 0⭐ 0
  • 💬 0🔄 0⭐ 2
  • Nov 24, 2026, 9:26 PM

    @mkoek @foone so obj.foo.bar turns into something like this in the AST:

    attribute-access bar (
    attribute-access foo (
    local-access obj
    )
    )

    local-access obj resolves fine, but the attribute-access foo doesn't because there's no attribute named foo on obj.

    💬 0🔄 0⭐ 1
  • Nov 24, 2026, 9:22 PM

    @foone yeeesh. Why? Was a leading underscore or __get_item__() not obscure enough? They really really wanted to force someone to use getattr()

    💬 0🔄 0⭐ 2
  • 💬 0🔄 0⭐ 0
  • Nov 24, 2026, 10:08 PM

    @foone getattr() or internal duct might work though. Could be fun to mix up bar.baz and bar.baz right...

    💬 0🔄 0⭐ 0
  • Nov 24, 2026, 10:19 PM

    @foone curious what happens if you do this to a key in sys.modules and then try to import it?

    💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • Nov 24, 2026, 10:58 PM

    @foone When I was an intern nearly 20 years ago, I had a funny experience with Matlab.

    I had to add features in a Matlab simulation script someone else started. At first it ran something like 1 hour. After I added a feature, it tool like 10 minutes.

    I spent 3 days searching what part of the simulation I accidentally deleted.

    💬 1🔄 0⭐ 2
  • Nov 24, 2026, 10:59 PM

    @foone The solution was: For a new feature, I needed the Matlab function "max". Originally, one variable (or array?) in the script was named "max" - what rendered the function "max" inaccessible. So I renamed the variable.
    The fact that there was a variable with the same name as a Matlab-internal function (that was read or written permanently) slowed Matlab down by factors.

    When telling the original developer, his reaction was: So I drank all that coffee waiting for the results for nothing?

    💬 0🔄 0⭐ 1
  • 💬 0🔄 0⭐ 0
  • Nov 25, 2026, 7:27 AM

    Wireshark JSON output contains keys with dots in their names (frame.number). Makes it pain to process output with jq. It is possible, but you need to quote most of keys.
    @foone

    💬 0🔄 0⭐ 0