Skip to main content

Low-level Python attribute access utilities that bypass the descriptor protocol.

Project description

Low-level Python attribute access utilities that bypass the descriptor protocol.

What is it?

rawattr provides utilities for exploring, getting, setting, and deleting attributes exactly as they physically appear in the __dict__ of Python objects and their classes - without triggering properties, methods, or any descriptor logic. This makes it easy to introspect and manipulate attributes "raw", without Python's usual attribute access rules.

  • No descriptor protocol: No property, method binding, or magic getters/setters—just pure dict lookups.
  • Access class and instance dicts: Traverse instance, then all classes in MRO (method resolution order).
  • Manipulate attributes on both instance and class-level, or just the instance.

Why?

Sometimes you want to see what is actually in the underlying attribute dicts, or manipulate attributes as stored, not as interpreted by Python's attribute machinery.

  • See the actual keys and values present (including methods and slot descriptors).
  • Set or delete attributes without triggering custom logic.
  • Useful for introspection tools, dynamic manipulation, advanced class/instance hacking, and more.

Installation

pip install rawattr

Usage

# coding=utf-8
from rawattr import raw_getattr, raw_setattr, raw_hasattr, raw_delattr


class S(object):
    __slots__ = ('s',)


raw_getattr(S, 's')  # <member 's' of 'S' objects>
raw_getattr(S(), 's')  # <member 's' of 'S' objects>


class A(object):
    x = 42


class B(A):
    pass


b = B()
b.y = 100

raw_hasattr(b, 'x')  # True: sees A.x on the class
raw_hasattr(b, 'y')  # True

raw_getattr(b, 'x')  # 42: unbound, as stored in class __dict__
raw_getattr(b, 'y')  # 100

raw_setattr(b, 'y', 1000, add_to_instance=True)
raw_getattr(b, 'y')  # 1000

raw_delattr(b, 'y')  # Deletes b.y
raw_hasattr(b, 'y')  # False

# AttributeError: 'B' object has no attribute 'y'
try:
    raw_getattr(b, 'y')
except AttributeError:
    pass

# TypeError: 'dictproxy' object does not support item assignment (Python 2)
# TypeError: 'mappingproxy' object does not support item assignment (Python 3)
try:
    raw_setattr(b, 'x', 77, add_to_instance=False)
except TypeError:
    pass

# TypeError: 'dictproxy' object does not support item deletion (Python 2)
# TypeError: 'mappingproxy' object does not support item deletion (Python 3)
try:
    raw_delattr(b, 'x')
except TypeError:
    pass

API Reference

yield_dicts_and_owners(obj)

Yield pairs of (dict, owner) for the instance (__dict__, obj) and each class in its MRO (class __dict__, class), if a __dict__ exists.

raw_hasattr(obj, attr)

True if attr is present as a key in either the instance or any of its class __dict__s (no descriptors).

raw_getattr(obj, attr, default=NO_DEFAULT)

Fetches the raw value, or raises (or returns default), without triggering descriptors or attribute methods.

raw_setattr(obj, attr, value, add_to_instance=True)

Sets an attribute—by default, adds to the instance's __dict__; if add_to_instance=False, only updates the attribute in the first (instance/class) __dict__ where it is present in-place.

raw_delattr(obj, attr)

Deletes the key from the first (instance/class) __dict__ where it is present.

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rawattr-0.1.0a0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rawattr-0.1.0a0-py2.py3-none-any.whl (4.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file rawattr-0.1.0a0.tar.gz.

File metadata

  • Download URL: rawattr-0.1.0a0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for rawattr-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 9a646f31058197a46124061744c89a4c4b9e03c3e650fded7f1dcec30610dbee
MD5 027d68b1c69fa304282dc5403f5ce516
BLAKE2b-256 1ad2420658efa98ed5ec78c13a56edbfc3cd3b94157d900fad8778e3caa2eb81

See more details on using hashes here.

File details

Details for the file rawattr-0.1.0a0-py2.py3-none-any.whl.

File metadata

  • Download URL: rawattr-0.1.0a0-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for rawattr-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9825442d06aa72edc873d2c271930e5d2e5ba5678e180f3b9e41abcce9792899
MD5 47992f6175ff72688e4c961f902cfe31
BLAKE2b-256 71e9ea4f2f5252a17d095b2530c99ae602b8dffe365f4e5000377f62cd028bd3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page