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,210
  • 4
  • 21
  • 35
Stateless
  • 263
  • 2
  • 3
  • 16

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,353
  • 3
  • 20
  • 38
1
def __init__(a,b,c='your default value')
Exprator
  • 25,406
  • 6
  • 40
  • 52