Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional helper #595

Merged
merged 5 commits into from
Feb 25, 2019
Merged

Add optional helper #595

merged 5 commits into from
Feb 25, 2019

Conversation

josephmancuso
Copy link
Member

@josephmancuso josephmancuso commented Feb 25, 2019

This is really cool helper that allows you to wrap any object in this helper and call attributes or methods on it even if they don't exist. If they exist then it will return the method, if it doesn't exist it will return None.

This gets around this code example:

def show(self):
    user = User.find(1)
    if user and user.id == 5:
        # do code
        ...

Now we can use this optional helper

def show(self):
    if optional(User.find(1)).id == 5:
        # do code
        ...

If the user id exists it will return the ID, else it will return None

@josephmancuso josephmancuso merged commit fcf2518 into master Feb 25, 2019
@josephmancuso josephmancuso deleted the add-optional-helper branch April 1, 2019 00:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant