Default values for instance variables in python

Default values for instance variables in python

Default values for instance variables in python

In Python, you can provide default values for instance variables in the __init__ method of a class. Instance variables are attributes that are unique to each instance of a class. By assigning default values in the constructor, you can ensure that instances are created with specific initial values for those attributes.

Here’s an example of using default values for instance variables:

Python
class Person:
    def __init__(self, name, age=18):
        self.name = name
        self.age = age

# Create instances with and without specifying age
person1 = Person("Alice")
person2 = Person("Bob", 25)

print(person1.name, person1.age)  # Output: Alice 18
print(person2.name, person2.age)  # Output: Bob 25

In this example, the Person class has an __init__ method that takes name and age as arguments, with age having a default value of 18. When you create instances of the class, you can provide values for both name and age, or just for name, in which case age will default to 18.

You can set default values for any instance variables by specifying them as default parameters in the constructor. If an argument is provided during object creation, it will override the default value; otherwise, the default value will be used.

Note that if a mutable object (such as a list or dictionary) is used as a default value, you need to be careful, as changes to that object will affect all instances that use the default. To avoid this, you can use None as the default and create a new instance of the mutable object inside the constructor if needed.

Python
class Example:
    def __init__(self, data=None):
        if data is None:
            data = []
        self.data = data

Using default values for instance variables can make your code more flexible and reduce the need for providing all arguments when creating instances of a class.

Exit mobile version