Created
June 27, 2018 12:16
-
-
Save gcrsaldanha/9e800051432d79f775485712d9f5985e to your computer and use it in GitHub Desktop.
This gist demonstrates the usage of `set()` and `{}` for building sets in python and when one is preferred over the other
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dis | |
def non_empty_set_literal(): | |
return {1, 2, 3} # Build a set {1, 2, 3} | |
def non_empty_set_method(): | |
return set([1, 2, 3]) # Builds a set {1, 2, 3} from a list | |
def empty_set_literal(): | |
return {} # Does NOT work => Generates an empty dict {} | |
def empty_set_method(): | |
return set() # Generates an empty set | |
dis.dis(non_empty_set_literal) | |
dis.dis(non_empty_set_method) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment