Python class and function json

时间:2021-11-14 02:21:58
# coding=utf-8
__author__ = 'student'
'''
how to define a class
how to extend a class
how to make a package
how to import package
how to define a method
'''
str = u"我是中国人"
print str class Calculator:
def __init__(self):
print "I am a Calculator"
def add(self,op1,op2):
return op1 + op2
def divide(self,op1,op2):
return op1/op2
def multiply(self,op1,op2):
return op1*op2
def subtract(self,op1,op2):
return op1-op2
def others(self,expr):
eval(expr) c = Calculator()
print c.add(1,2)
print c.subtract(2,10)
print c.others("3*5")
print eval ("3*5") name = dict(first='Bob', last='Smith')
rec = dict(name=name, job=['dev', 'mgr'], age=40.5)
print rec
import json
S=json.dumps(rec)
print S
json.dump(rec, fp=open('testjson.txt', 'w'), indent=4)
print(open('testjson.txt').read())
p=json.load(open("testjson.txt"))
print p for x in xrange(1,10,2):
print x