Shanshan Pythoner Love CPP

Python Cheatsheet: Basic

2017-02-11

Name Rule

# for class
MyClass
# for function, module, package, variables
var_separate
# for public
var
# for private
__var
# for internal use
_var
# for protect
_var_
# convention to avoid conflict keyword
var_
# "magic" method or attributes
__var__

future

# backport python3 print_function in python2
from __future__ import print_function
print "hello world" # errors

# backport python3 unicode_literals in python2
from __future__ import unicode_literals
print type("Guido")

# Changing the Division Operator
from __future__ import division
print 1/2	#float
print 1//2 #int

Define function doc

def Example():
	""" This is an example function """
	print "example func"

print Example.__doc__
# or
help(Example)

Check, Get, Set attribute, inheritance in Class

class Example(object):
	def __init__(self):
		self.name = "ex"
   	def printex(self):
     	print "This is an example"
ex = Example()
print hasattr(ex, "name")
print getattr(ex,'name')
setattr(ex, name','example')
print issubclass(Example, object)

Check global variables

print globals()

Get Function / Class name

print ex.__class__.__name__

Diamond Problem

# The problem of multiple inheritance in searching a method
def foo_a(self):
	print("This is ClsA")
def foo_b(self):
	print("This is ClsB")
def foo_c(self):
	print("This is ClsC")	

class Type(type):
	def __repr__(cls):
		return cls.__name__

ClsA = Type("ClsA", (object,), {'foo': foo_a})
ClsB = Type("ClsB", (ClsA,), {'foo': foo_b})
ClsC = Type("ClsC", (ClsA,), {'foo': foo_c})
ClsD = Type("ClsD", (ClsB, ClsC), {})
ClsD.mro()
ClsD().foo()				

Smart List

a = [1, 2, 3, 4, 5]
print a[0]
print a[:-1]
print a[0:-1:2]
# copy
ex = [0] * 10
# extend
ex = [1, 2, 3] + ['a', 'b']
# get index and item in the loop
for idx, item in enumerate(a):
	print (idx, item),

Transfer two list into tuple list

zip(a, a)
# with filter
[x for x in a if x>1]
# get distinct val
list(set(a))
# reverse a list
b = a[::-1]

Smart Dictionary

a = {"1":1, "2":2, "3":3}
b = {"2":2, "3":3, "4":4}
print a.keys()
print a.itemes()
# find same key between two dictionary
[_ for _ in a.keys() if _ in b.keys()
# another way
c = set(a).intersection(set(b))
# update dictionary
a.update(b)

for else

for _ in range(5):
	print _,
else:
	print "\n no breaks"

try else

try:
	print "No exception"
except:
	pass
else:
	print "No exception occurred"

lambda function

fn = lambda x: x**2

Option arguments

def example(a, b=None, *args, **kwargs):
	print a, b
	print args
	print kwargs
example(1, "var", 2, 3, word="hello")

Comments

Content