Python built-in attributes are available on objects and classes.
In Python, there are several built-in attributes that are available on objects and classes. These attributes provide useful information or functionality that can be accessed or utilized in your code. Here are some commonly used built-in attributes:
__doc__
:
- It is an attribute that holds the documentation string (docstring) of a module, class, function, or method.
- Example:
def my_function(): """This is a docstring.""" pass print(my_function.__doc__) # Output: This is a docstring.
__name__
:
- It is an attribute that holds the name of a module, function, class, or method.
- Example:
import math print(math.__name__) # Output: math
__class__
:
- It is an attribute that refers to the class of an instance object.
- Example:
class MyClass: pass obj = MyClass() print(obj.__class__) # Output: <class '__main__.MyClass'>
__module__
:
- It is an attribute that holds the name of the module in which a class or function is defined.
- Example:
import math print(math.sqrt.__module__) # Output: math
__dict__
:
- It is an attribute that holds the namespace of an object. It is a dictionary containing the object’s attributes.
- Example:
class MyClass: attribute = 10 obj = MyClass() print(obj.__dict__) # Output: {'attribute': 10}
__file__
:
- It is an attribute that holds the name of the file from which a module was loaded.
- Example:
import math print(math.__file__) # Output: /path/to/python/lib/math.py
__len__
:
- It is an attribute that allows an object to define its length when using the
len()
function. - Example:
my_list = [1, 2, 3] print(len(my_list)) # Output: 3
__iter__
:
- It is an attribute that enables an object to be iterable. It returns an iterator object that can be used to iterate over the elements of the object.
- Example:
my_list = [1, 2, 3] my_iter = iter(my_list) print(next(my_iter)) # Output: 1
__next__
:
- It is an attribute used in conjunction with the
__iter__
attribute to define the behavior of the iterator. It returns the next item in the iteration. - Example:
class MyIterator: def __init__(self, data): self.data = data self.index = 0 def __iter__(self): return self def __next__(self): if self.index >= len(self.data): raise StopIteration item = self.data[self.index] self.index += 1 return item my_iter = MyIterator([1, 2, 3]) for item in my_iter: print(item) # Output: 1, 2, 3
__call__
:
- It is an attribute that allows an object to be called as a function. It enables instances of a class to behave like functions.
- Example:
class Adder: def __call__(self, a, b): return a + b add = Adder() result = add(2, 3) print(result) # Output: 5
__getitem__
and __setitem__
:
- These attributes allow objects to support indexing and item assignment using square brackets (
[]
). - Example:
class MyList: def __init__(self): self.items = [] def __getitem__(self, index): return self.items[index] def __setitem__(self, index, value): self.items[index] = value my_list = MyList() my_list[0] = 42 print(my_list[0]) # Output: 42
Conclusion:
Each attribute provides specific information or functionality that can be accessed or utilized to enhance your code. The availability and usage of built-in attributes may vary depending on the type of object or class being used.