1

Is there any way to simplify the following code:

def get_or_create_user(id, photo=None, first_name=None, last_name=None, nickname=None, organization=None, city=None, country=None):
    ... # save user in the datastore

class UserCreationHandler(webapp.RequestHandler):
    def get(self):
        id = self.request.get('id')
        photo = None
        first_name = None
        last_name = None
        nickname = None
        organization = None
        city = None
        country = None
        if something1:
            # do some calculations
            if something2:
                # do some other calculations
                if something3:
                    # assign id, photo, first_name etc. values 
        get_or_create_user(id, photo, first_name, last_name, nickname, organization, city, country)

I don't like these assignments to None.

2 Answers 2

6

Something like:

def get(self):
    id = self.request.get('id')
    user_details = {}
    if something:
        # assign user_details['id'], user_details['photo'], user_details['first_name'] etc. values
    get_or_create_user(id, **user_details)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. What is the purpose of these **?
@LA_ ** expands a dict to named arguments when calling a function. Similarly, * expands a list for positional arguments.
3

Seeing that get_or_create_user uses default arguments set to None:

def get_or_create_user(id, photo=None, first_name=None, last_name=None, nickname=None, organization=None, city=None, country=None):

You can just use this:

class UserCreationHandler(webapp.RequestHandler):
    def get(self):
        id = self.request.get('id')
        if something:
            # set stuff ...
            # and then call get_or_create_user(...)
        else:
            get_or_create_user(id)

1 Comment

Sorry, actually there are more IF conditions - I've updated the code.

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.