Junior — Middle
What is the difference between init and dir functions in Python?
sobes.tech AI
Answer from AI
__init__ — this is a special method in Python used for initializing a new class object. It is called automatically when an instance is created and is usually used to set initial attribute values.
dir() — this is a built-in Python function that returns a list of an object's attributes and methods (or the current scope if no argument is passed). It helps to find out what properties are available on the object.
Example:
class MyClass:
def __init__(self, value):
self.value = value
obj = MyClass(10)
print(dir(obj)) # Will output a list of attributes and methods of the obj object
Summary:
__init__— method for object initialization.dir()— function to get a list of an object's attributes.