Miscellaneous Notes#
Fundamentals
Data Structure
Program Structure
Input and Output
Classes and Objects
## How to check current object sizes in Notebook
import sys
a = range(10000)
print(sys.getsizeof(a))
48
## Get the docstring
?sys.getsizeof
?who
%who
%whos
globals()
{'__name__': '__main__',
'__doc__': 'Automatically created module for IPython interactive environment',
'__package__': None,
'__loader__': None,
'__spec__': None,
'__builtin__': <module 'builtins' (built-in)>,
'__builtins__': <module 'builtins' (built-in)>,
'_ih': ['',
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')\nglobal()",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')\nglobals()",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')\nglobals()\nlocals()",
'globals()'],
'_oh': {4: {...}, 5: {...}},
'_dh': ['/Users/Alvin/Dropbox/Python/PythonTricksTheBook/PythonTricksJupyterBook'],
'In': ['',
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')\nglobal()",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')\nglobals()",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')\nglobals()\nlocals()",
'globals()'],
'Out': {4: {...}, 5: {...}},
'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7ff579662588>>,
'exit': <IPython.core.autocall.ZMQExitAutocall at 0x7ff57969aa20>,
'quit': <IPython.core.autocall.ZMQExitAutocall at 0x7ff57969aa20>,
'_': {...},
'__': {...},
'___': '',
'_i': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos\nglobals()\nlocals()',
'_ii': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos\nglobals()',
'_iii': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos\nglobal()',
'_i1': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos',
'a': range(0, 10000),
'_i2': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos',
'_i3': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos\nglobal()',
'_i4': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos\nglobals()',
'_4': {...},
'_i5': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos\nglobals()\nlocals()',
'_5': {...},
'_i6': 'globals()'}
locals()
{'__name__': '__main__',
'__doc__': 'Automatically created module for IPython interactive environment',
'__package__': None,
'__loader__': None,
'__spec__': None,
'__builtin__': <module 'builtins' (built-in)>,
'__builtins__': <module 'builtins' (built-in)>,
'_ih': ['',
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')\nglobal()",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')\nglobals()",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')\nglobals()\nlocals()",
'globals()',
'locals()',
'locals()'],
'_oh': {4: {...}, 5: {...}, 6: {...}, 7: {...}},
'_dh': ['/Users/Alvin/Dropbox/Python/PythonTricksTheBook/PythonTricksJupyterBook'],
'In': ['',
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')\nglobal()",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')\nglobals()",
"#import sys\na = range(10000)\n#%memusage a\nget_ipython().run_line_magic('who', '')\nget_ipython().run_line_magic('whos', '')\nglobals()\nlocals()",
'globals()',
'locals()',
'locals()'],
'Out': {4: {...}, 5: {...}, 6: {...}, 7: {...}},
'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7ff579662588>>,
'exit': <IPython.core.autocall.ZMQExitAutocall at 0x7ff57969aa20>,
'quit': <IPython.core.autocall.ZMQExitAutocall at 0x7ff57969aa20>,
'_': {...},
'__': {...},
'___': {...},
'_i': 'locals()',
'_ii': 'globals()',
'_iii': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos\nglobals()\nlocals()',
'_i1': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos',
'a': range(0, 10000),
'_i2': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos',
'_i3': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos\nglobal()',
'_i4': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos\nglobals()',
'_4': {...},
'_i5': '#import sys\na = range(10000)\n#%memusage a\n%who\n%whos\nglobals()\nlocals()',
'_5': {...},
'_i6': 'globals()',
'_6': {...},
'_i7': 'locals()',
'_7': {...},
'_i8': 'locals()'}
dir()
['In',
'Out',
'_',
'_4',
'_5',
'_6',
'_7',
'_8',
'__',
'___',
'__builtin__',
'__builtins__',
'__doc__',
'__loader__',
'__name__',
'__package__',
'__spec__',
'_dh',
'_i',
'_i1',
'_i2',
'_i3',
'_i4',
'_i5',
'_i6',
'_i7',
'_i8',
'_i9',
'_ih',
'_ii',
'_iii',
'_oh',
'a',
'exit',
'get_ipython',
'quit']