3

What's the most pythonic or elegant way of achieving this?

def __init__(self, connection=None, some=None, thing=None, else=None):
   if connection is None:
      self.connection = SetConnection()
   else:
      self.connection = connection
.
.

If I have multiple input args like above for which I would like to call another class to instantiate. Any good way to keep it clean looking without being verbose?

3
  • 1
    I keep find it amazing how it is always the "simple" questions that raise so much traffic and ideas Commented Dec 13, 2018 at 21:03
  • you can use isinstance to check if connection is indeed the class/type you need and not something else. Commented Dec 13, 2018 at 21:23
  • 1
    @DeepSpace Indeed: en.wikipedia.org/wiki/KISS_principle Commented Dec 13, 2018 at 21:51

5 Answers 5

4

You could use a binary operator:

def __init__(self, connection=None, some=None, thing=None, else=None):
    self.connection = connection or SetConnection()

If connection is None (False) it will run SetConnection().

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

9 Comments

This will only work as long as False, 0, empty list etc are not possible values. A single-liner that solves this is self.connection = connection if connection is not None else SetConnection()
Note that this assumes that all valid values of the parameter are truthy.
True, this does make assumptions based on input. Though he was asking for "Any good way to keep it clean looking without being verbose"
While this approach has its limitations (indeed, it won't work if connection can have a valid non-None value that is not truthy, or if the SetConnection class defines __bool__ in an unusual way), it's also in my experience very idiomatic. I've definitely seen a lot of code use or rather than a ternary expression for cases like this.
It makes sense when you take his question's confinements into consideration. Thanks for taking my side, or has it's weakneses but it does make for "clean looking" code and this is what he asked for
|
1

Use ternary operator

def __init__(self, connection=None, some=None, thing=None, else=None):
   self.connection = SetConnection() if connection is None else connection

1 Comment

Although other answer shows "simpler" solution, I ended up using this as it will not mislead when inputs are 0, False, [], (), {} and ''
1

If you don't need to record, for any further reason, the fact that connection was originally passed as None, then you could overwrite that name:

if connection is None: connection = SetConnection()
self.connection = connection

1 Comment

While this is a valid syntax, please don't format your code like this. Put that inner line on it's own, indented line. Compound statements are generally discouraged
0

My 2c using isinstance :

def __init__(self, connection=None, some=None, thing=None, else=None):
   if isinstance(connection, "class/type of connection") :
       self.connection = connection    
   else:
       self.connection = SetConnection()

Old answer:

self.connection = connection if connection else SetConnection()

PS: I'm not aware of the class or type of connection, if you also don't know it, usetype(connection)

2 Comments

Same debate applies as to Jaba's answer: this is probably OK given the context, but it makes the unstated assumption that 0, False, [], (), {} and '' are not among the valid possibilities for connection.
Noted. One solution would be using isinstance to assure connection is the instance we expect it to be.
0

Just for fun, why not do

def __init__(self, **kwargs):
    self.connection = kwargs.get('connection', SetConnection())

Which puts aside problems related to falsy values while being pythonic. That being said, it has other drawbacks for sure, e.g. those related to the idea of auto-documentability of the code.

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.