0

I am new in python and please excuse me if my question is a basic question or is not clear.

I have a class in python and I want to set some attributes for my objects whenever I generate my objects from this class. I know, I must use __init__ method for this purpose.

The problem is that I want to have this ability that I can set my attributes in two ways. For some of my objects, I want to pass some values to __init__ method and set the attributes with those values. In addition, for some objects, I want to set some default values for attributes of my objects in __init__ method.

My question: How can I have an __init__ method that for some objects uses passed values for setting its attributes, and for some other objects, that are not passed, use default values?

elena
  • 3,740
  • 5
  • 27
  • 38
Stateless
  • 293
  • 2
  • 4
  • 18

2 Answers2

1

Simply equate the the parameters that you would want to be defaulted to their default value.

For instance, __init__(a,b,c=0) would give 0 as the default value to c which you can override by passing another value when object is created.

Ananda
  • 2,925
  • 5
  • 22
  • 45
1
def __init__(a,b,c='your default value')
Exprator
  • 26,992
  • 6
  • 47
  • 59