0

I want the user to be able to initiate a class by passing an argument to it, and if he doesn't pass it then it should be automatically created by the class. How is that usually done in Python? Example:

class MyClass(object):

    def __init__(self, argument):

        self.argm = argument
        # logic here: if user does not pass argument
        # run some function or do something


    def create_argm(self):

        self.argm = 'some_value'



object_example = MyClass()
print(object_example.argm) # will print 'some_value'

object_example = MyClass('some_other_value')
print(object_example) # will print 'some_other_value'

Edit : self.argm will be a python-docx Object so i'm unable to do def __init__(self, argument = Document() or am i?

3
  • 3
    def __init__(self, argument='some_value'): Commented Jun 7, 2018 at 6:34
  • 1
    Since you already know how to create default arguments, this should answer your question: Python optional parameters Commented Jun 7, 2018 at 6:40
  • @Aran-Fey thx for the link, i'll initialise with argument=None and check it's value inside init. Thx! Commented Jun 7, 2018 at 6:46

2 Answers 2

3

if you cant create the value in the function definition, you can use a value that indicates nothing, luckily python has None so you can do something like:

class MyClass(object):
    def __init__(self, argument=None):
        if argument is None:
            self.argm = self.create_argm()
        else:
            self.argm = argument

    def create_argm(self):
        return 'some_value'

if None Doesn't fit because you want that to be a valid value for argument without assuming it was left out you can always create a dummy value:

class MyNone:
    pass

class MyClass(object):
    def __init__(self, argument=MyNone):
        if argument is MyNone:
            self.argm = self.create_argm()
        else:
            self.argm = argument

    def create_argm(self):
        return 'some_value'
Sign up to request clarification or add additional context in comments.

3 Comments

The usual pattern for creating objects like MyNone is sentinel = object(); creating a class is a bit overkill.
Can't we replace the if else(in the first case) with simple or. ie, self.argm = argument or self.create_argm(). May be I am wrong?
@abdul_niyas_pm that depends, do you know the value you're receiving will always evaluate to True? if yes then you can, but in general its better not to take the risk, this sort of code is in a lot of built in functions and packages and the reason is that it is reliable
2

This is usually done with a default value assigned to a key word argument:

class MyClass(object):

    def __init__(self, argument='default value'):
        self.argm = argument

You have to pay special attention if you want this default value to be a mutable object; this may lead to unwanted behavior, as the object will be created only once, then mutated.

2 Comments

It is a mutable object, a python-docx object. I think i have an idea what you imply by unwanted behavior but i'm not sure. Thx for the answer anyway!
I added a short explanation - if you search stack overflow, you will find the long answer.

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.