Skip to main content

objname

A library with a base class that stores the assigned name of an object.

>>> import objname
>>> x, y = objname.AutoName()
>>> x.name
'x'
>>> y.name
'y'

Official documentation at readthedocs: https://objname.readthedocs.io/en/latest/

Table Of Contents

Requirements

objname requires Python 3.6 or newer. It has no third-party dependencies and works on both POSIX and Windows. It runs in cPython and PyPy.

Installation

To install it just use pip:

$ pip install objname

You can also install it from github:

$ pip install git+https://github.com/AlanCristhian/objname.git

Tutorial

objname has only one class: AutoName. It creates an object with the objname attribute that stores the name of such object. E.g:

>>> import objname
>>> a = objname.AutoName()
>>> a.name
'a'

It can make multiple variables with iterable unpacking syntax.

>>> import objname
>>> x, y = objname.AutoName()
>>> x.name
'x'
>>> y.name
'y'

You can make your own subclass that inherit from objname.AutoName.

>>> import objname
>>> class Number(objname.AutoName):
...     def __init__(self, value):
...         super().__init__()
...         self.value = value
...
>>> a = Number(1)
>>> a.name
'a'
>>> a.value
1

Observations

How it works

AutoName searches the name of the object in the bytecode of the frame where the object was created. If it can't find a name, then the default '<nameless>' value are set.

Multiple assignment syntax

AutoName stores the last name in the expression.

>>> import objname
>>> a = b = objname.AutoName()
>>> a.name
'b'
>>> b.name
'b'

That is the same behaviour of __set_name__ method.

>>> class SetName:
...     def __set_name__(self, owner, name):
...         self.name = name
...
>>> class MyClass:
...     a = b = SetName()
...
>>> MyClass.a.name
'b'
>>> MyClass.b.name
'b'

API reference

class AutoName()

Stores the assigned name of an object in the name attribute.

Single assignment:

>>> obj = AutoName()
>>> obj.name
'obj'

Iterable unpacking syntax:

>>> a, b = AutoName()
>>> a.name
'a'
>>> b.name
'b'

Contribute

Donation

Buy Me a Coffee 🙂: https://www.paypal.com/donate?hosted_button_id=KFJYZEVQVRQDE

License

The project is licensed under the MIT license.

Metadata

Release files for objname 0.12.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for objname 0.12.1
File Interpreter ABI Platform
objname-0.12.1-py3-none-any.whl Python 3 none any Details

Release files / objname-0.12.1-py3-none-any.whl

Download URL objname-0.12.1-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d565cbce91c048021f9328c33e6d6ddd266edafe6ddfabaea1e746a103af30a9
BLAKE2b-256 checksum
How to use checksums
4b05fb5cc77d669f9b28b1ec5d930f64c5576b4d9d9e436e30ba1d12b8838615
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.0rc1

Release history Release notifications | RSS feed

This release

0.12.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page