码迷,mamicode.com
首页 > 编程语言 > 详细

Python例子二

时间:2015-03-06 10:27:19      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

例1、构造函数

#-*-coding:utf-8-*-
import sys
class Student:
    def __init__(self,name,age):
        self.__name=name
        self.__age=age
    def getName(self):
        format="my name is %s my age is %d"%(self.__name,self.__age)
        print format
    def __del__(self):
        print "del"
if __name__=="__main__":
   studeng=Student("chu",35)
   studeng.getName()

例二:静态成员与私有成员

#-*-coding:utf-8-*-
import sys
class A:
    y=2
    def __init__(self):
        self.x=1
        self.__z=1
if __name__=="__main__":
    a=A()
    print a.x
    print A.y
    print a.__z

例三:

#-*-coding:utf-8-*-
import sys
class A:
    def prt(self):
        print "my name is A"
    def reprt(self):
        A.prt(self)
if __name__=="__main__":
    a=A()
    a.prt()
    a.reprt()

 例四:字典的散列查找

#-*-coding:utf-8-*-
import sys
if __name__=="__main__":
    dict={"a":"apple","b":"banana","g":"grape","o":"orange"}
    print dict
    print dict["a"]
    dict2={1:"apple",2:"banana",3:"grape",4:"orange"}
    print dict2
    print dict2[1]

 

Python例子二

标签:

原文地址:http://www.cnblogs.com/bluewelkin/p/4317352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!