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?

3

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1
def __init__(a,b,c='your default value')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.