From 62c42349d3ba86cfdfb0340d8b0204a551a7c905 Mon Sep 17 00:00:00 2001 From: a <139073260+Azeem000011111111@users.noreply.github.com> Date: Tue, 31 Oct 2023 23:26:08 +0500 Subject: [PATCH] Update README.md --- 14_class(OOPAdvance)/README.md | 196 ++++++++++++++++++++++++++++++++- 1 file changed, 195 insertions(+), 1 deletion(-) diff --git a/14_class(OOPAdvance)/README.md b/14_class(OOPAdvance)/README.md index 0757c2e..d6484ba 100644 --- a/14_class(OOPAdvance)/README.md +++ b/14_class(OOPAdvance)/README.md @@ -145,7 +145,201 @@ print(isinstance(obj, object)) # Output: True ``` In this example, `MyClass` implicitly inherits from `object`, so an instance of `MyClass` is also an instance of `object`. -``` +`` +# Dunder-Methods + +## some common dunder methods in python and their usage + +
Dunder method | +Description | +Example | +
---|---|---|
`__new__()` | +This method is called before any other constructor method is called. It is used to create a new object. | +`class MyClass(object): def __new__(cls, *args, **kwargs): # Create a new object obj = object.__new__(cls) # Initialize the object obj.__init__(*args, **kwargs) return obj` | +
`__init__()` | +This method is called to initialize a new object. It is typically used to set the attributes of the object. | +`class MyClass(object): def __init__(self, name): self.name = name` | +
`__del__()` | +This method is called when an object is garbage collected. It can be used to clean up any resources that the object is using. | +`class MyClass(object): def __del__(self): # Clean up any resources print("Deleting object {}".format(self.name))` | +
`__repr__()` | +This method is called when the `repr()` function is used on an object. It returns a string representation of the object. | +`class MyClass(object): def __repr__(self): return "MyClass('{}')".format(self.name)` | +
`__str__()` | +This method is called when the `str()` function is used on an object. It returns a string representation of the object that is intended to be human-readable. | +`class MyClass(object): def __str__(self): return "MyClass object with name '{}'".format(self.name)` | +
`__getattr__()` | +This method is called when an attribute is accessed on an object that does not exist. It can be used to implement dynamic attribute lookup. | +`class MyClass(object): def __getattr__(self, name): # Get the attribute from a dictionary return self._dict[name]` | +
`__setattr__()` | +This method is called when an attribute is set on an object. It can be used to implement validation or other logic before setting the attribute. | +`class MyClass(object): def __setattr__(self, name, value): # Validate the value if not isinstance(value, str): raise ValueError("Attribute {} must be a string".format(name)) # Set the attribute self._dict[name] = value` | +
`__delattr__()` | +This method is called when an attribute is deleted from an object. It can be used to implement cleanup logic. | +`class MyClass(object): def __delattr__(self, name): # Delete the attribute from a dictionary del self._dict[name]` | +
`__call__()` | +This method is called when an object is called like a function. It can be used to implement custom behavior for calling an object. | +`class MyClass(object): def __call__(self): # Print a message print("You called me!")` | +
`__getitem__()` | +This method is called when an item is accessed on an object using the `[]` operator. It can be used to implement custom behavior for accessing items from an object. | +`class MyClass(object): def __getitem__(self, index): # Get the item from a list return self._list[index]` | +
`__setitem__()` | +This method is called when an item is set on an object using the `[]` operator. It can be used to implement custom behavior for setting items in an object. | +`class MyClass(object): def __setitem__(self, index, value): # Set the item in a list self._list[index] = value` | +
`__delitem__()` | +This method is called when an item is deleted from an object using the `del` statement. It can be used to implement cleanup logic. | +`class MyClass(object): def __delitem__(self, index): # Delete the item from a list del self._list[index]` | +
`__iter__()` | +This method is called when an object is iterated over. It returns an iterator for the object. | +`class MyClass(object): def __iter__(self): # Return an iterator for the list return iter(self._list)` | +
`__next__()` | +This method is called when the `next()` function is used on an iterator. It returns the next item in the iterator. | +`class MyClass(object): def __next__(self): # Return the next item in the list item = next(self._iter) # Check if the item is the last item in the list if item is None: raise StopIteration()` | +