Python class and function json

# 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

posted on 2016-03-06 11:29  tneduts  阅读(266)  评论(0编辑  收藏  举报

导航