getattr(object,name,default=None) function - builtin getattr replacement


Keywords
getattr, python
License
BSD-3-Clause
Install
pip install getattr==0.0.34

Documentation

Install

`[sudo] pip install getattr`

Features

  • builtin function getattr replacement with same syntax
  • extended AttributeError messages if attr not exists

Usage

>>> from getattr import getattr

>>> getattr(object,name,default=None)

Examples

# module
import os
>>> builtins.getattr(os,"not-existing")
AttributeError: 'module' object has no attribute 'not-existing'
>>> getattr.getattr(os,"not-existing")
AttributeError: module 'os' has no attribute 'not-existing'

# function
>>> builtins.getattr(function,"not-existing")
AttributeError: 'function' object has no attribute 'not-existing'
>>> getattr.getattr(function,"not-existing")
AttributeError: function '__main__.function' has no attribute 'not-existing'

# class
>>> class CLS: pass

>>> builtins.getattr(CLS,"not-existing")
AttributeError: type object 'CLS' has no attribute 'not-existing'
>>> getattr.getattr(CLS,"not-existing")
AttributeError: class '__main__.CLS' has no attribute 'not-existing'

# class object
>>> builtins.getattr(CLS(),"not-existing")
AttributeError: 'CLS' object has no attribute 'not-existing'
>>> getattr(CLS(),"not-existing")
AttributeError: __main__.CLS object has no attribute 'not-existing'

GitHub followers GitHub issues