question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attribute Error using `__setattr__` on Windows

See original GitHub issue

When running the following code, it works fine on Python 3.6 on Ubuntu 18.02 but not Windows 10.

>>> import wrapt
>>> a = wrapt.ObjectProxy(None)
>>> a.__setattr__('__dict__', {})

It errors when trying to set the attribute:

elif hasattr(type(self), name):
>           object.__setattr__(self, name, value)
E           AttributeError: can't set attribute

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
GrahamDumpletoncommented, Apr 27, 2019

The original problem and difference you saw is that __dict__ can only be changed on a Python class implemented as Python code. The ObjectProxy can be pure Python code, or C code depending on whether compiler was present so that C code implementation could be installed. Trying to update it when it was implemented as C code will fail.

0reactions
cdgriffithcommented, Apr 27, 2019

Okay I think I have a clearer idea now what I need to do. (I am new to wrapt and this code was recently accepted into a project I own so trying to nail down how best for it to work. Sorry for any confusion!) The code was trying to set __dict__ even when an object type that could be wrapped did not have that property.

Thank you so much for the quick guidance!

Just as an aside, It’s actually possible to set __dict__ to dict or any dict subclass.

from box import Box  # Box is a subclass of dict

class A:
    pass

h = A()

print(h.__dict__)
# {}
h.__dict__ = []
# Traceback (most recent call last):
#  File "<input>", line 1, in <module>
# TypeError: __dict__ must be set to a dictionary, not a 'list'
h.__dict__ = Box()
print(h.__dict__)
# <Box: {}>
Read more comments on GitHub >

github_iconTop Results From Across the Web

AttributeError when using object.__setattr__ - Stack Overflow
I need an object having a single attribute (like foo) should I define a class (like FooClass) just for it? python · object...
Read more >
SetAttr statement (VBA) | Microsoft Learn
A run-time error occurs if you try to set the attributes of an open file. Example. This example uses the SetAttr statement to...
Read more >
[Solved] AttributeError: can't set attribute in python - Finxter
The easiest way to fix the AttributeError:can't set attribute is to create a new namedtuple object with the namedtuple._replace() method.
Read more >
What is setattr in Python used for? - Toppr
When you try to call an attribute of an object whose type does not support that function, Python throws an AttributeError. In Python,...
Read more >
"legend_label" unexpected attribute error (works in Windows ...
I am running into an issue with the “legend_label” attribute. Curiously enough, it works fine when I run it on my Windows system, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found