Skip to content

Instantly share code, notes, and snippets.

@raja-ashok
Last active June 14, 2019 14:12
Show Gist options
  • Save raja-ashok/7a34c27c9b4141f0818a77a52996ac39 to your computer and use it in GitHub Desktop.
Save raja-ashok/7a34c27c9b4141f0818a77a52996ac39 to your computer and use it in GitHub Desktop.
Python CheatSheet
1. Main Function
if __name__ == "__main__":
2. CLI arg count
len(sys.argv)
2.1 CLI args
sys.arg[0]
sys.arg[1]
3. Basic
3.1 Variable
var = 15
g_var = 10
def func1():
global g_var
print str(g_var)
3.2 Shell cmd call
import os
os.system("cmd")
3.3 File Operations
f = open("test.txt",'w')
f.write("This file\n\n")
f.close()
4. Function
def func1(arg1, arg2):
...
4.1 Get Function name within that function
import inspect
def foo():
print(inspect.stack()[0][3])
5. Class with constructor and members
class ABC(object):
def __init__(self, arg1, arg2): # Constructor
self.a1 = 10 #Member Variable
...
def mem_func1(self): #Member function
...
def mem_func2(self):
...
def __del__(self): # Destructor
...
5.1 Releasing object
obj = None # Assigning None to any object will free it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment