0

I'm trying to associate additional data with a Python list. Ideally, I'd like to add custom attributes to a list object, similar to how you'd add attributes to a custom class. For example:

a = [1, 2, 4, 8]
a.x = "Hey!" # I'd like this to work without errors

However, when I attempt this, I get the following error:

AttributeError: 'list' object has no attribute 'x'

I understand that Python lists don't support adding arbitrary attributes out of the box. I also tried using setattr, but it gave me a similar error:

setattr(a, "k", 98) # Raises AttributeError

I cannot create a custom class DummyList(list) to work around this (for reasons). I also don't want to use some global variable that holds the attributes for the lists. Are there any workarounds to achieve this in Python?

asked Oct 10, 2023 at 23:15
8
  • 4
    What's the end goal? Beware the XY problem Commented Oct 10, 2023 at 23:17
  • 2
    Let's put it this way: in decades of programming, I have never needed to add attributes to a list instance, nor otherwise needed to bend over backwards to make a builtin do something it's clearly not designed to do. There are probably other, better, more out-of-the-box ways to do what you need to. Commented Oct 10, 2023 at 23:20
  • 3
    What are the reasons for not creating a class? If it's because inheriting from the builtins is messy, you can use UserList instead. Commented Oct 10, 2023 at 23:20
  • 1
    So you want to "smuggle" some metadata through a library together with your data, while the library doesn't explicitly support any such thing? And it's written in a hostile way that makes this smuggling of metadata difficult? Then yeah, I'd second the notion to subclass and override that library in a way that makes it possible to pass your own types with your own metadata through. If that proves too difficult because you'd need to override a ton of code, then I'd try to associate the data externally somehow, not passing it through at all. We'll need code samples for concrete advice. Commented Oct 11, 2023 at 2:33
  • 1
    Note that there's nothing wrong with your current solution of using a global dict with list identities as keys. It may not be as elegant as an idealistic yet unfortunately unattainable list with a custom attribute, but is simple and effective, and may be the best alternative if too much code needs to be overriden to make the aforementioned override approach work. Commented Oct 11, 2023 at 7:22

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.