Skip to content

Instantly share code, notes, and snippets.

@tmastny
Created July 6, 2020 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmastny/d17ed7bd1f56569fe8a50537d333ee36 to your computer and use it in GitHub Desktop.
Save tmastny/d17ed7bd1f56569fe8a50537d333ee36 to your computer and use it in GitHub Desktop.
# dynamically objects to completion
class Shape3():
area = 1
perimeter = 1
location = 1
shape_dict = {}
def __getitem__(self, key):
if hasattr(self, key):
return getattr(self, key)
return self.shape_dict[key]
def _ipython_key_completions_(self):
return ["area", "perimeter", "location"] + list(self.shape_dict)
def add(self, key):
self.shape_dict[key] = True
s = Shape3()
s.add("new_key")
# s["new<TAB>
s["new_key"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment