Python例2

15407 ワード

例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()

例2:静的メンバーとプライベートメンバー
#-*-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

例3:
#-*-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()

例4:辞書のハッシュ検索
#-*-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]

例5:静的方法
#-*-coding:utf-8-*-

import sys

class A:

    def prt(self):

        print "my name is A"

    def reprt(self):

        A.prt(self)

    @staticmethod

    def prt2():

        print "      "

if __name__=="__main__":

    a=A()

    a.prt()

    a.reprt()

    A.prt2()

例六:外来モジュールを呼び出す
#-*-coding:utf-8-*-

import sys

def func():

    print "hello ,  "

class MyClass:

    def myFunc(self):

        print "myModule.Myclass.myFunc()"
#-*-coding:utf-8-*-

import sys

import random

from _ctypes_test import func

from test32 import func



if __name__=="__main__":

    func()

例7:単一継承
#-*-coding:utf-8-*-

import sys

class A:

    x=1

class B(A):

    y=2

if __name__=="__main__":

    print B.x

    print B.y

例8:
#-*-coding:utf-8-*-

import sys

class SchoolMember:

    '''Represents any school member.'''

    def __init__(self,name,age):

        self.name = name 

        self.age = age

        print 'Initialized SchoolMember :%s'%self.name

    

    def tell(self):

        '''Tell my details.'''

    print 'Name:"%s" Age:"%s"' % (self.name,self.age)

class Teacher(SchoolMember):

    '''Represents a strudent.'''

    def __init__(self,name,age,salary):

        SchoolMember.__init__(self, name, age)

        self.salary =salary

        

        print '(Initialized Student : %s)' %self.name

    def tell(self):

        SchoolMember.tell(self)

        print 'Marks:"%d"'% self.salary

class Student(SchoolMember):

    '''Represents a strudent.'''

    def __init__(self,name,age,marks):

        SchoolMember.__init__(self, name, age)

        self.marks =marks

        

        print '(Initialized Student : %s)' %self.name

    def tell(self):

        SchoolMember.tell(self)

        print 'Marks:"%d"'% self.marks

if __name__=="__main__":

    t = Teacher('Mrs.Shrividya',40,30000)

    s = Student('Swaroop',22,75)

    

例9:再帰ディレクトリ、ファイルの変更
#-*-coding:utf-8-*-

import sys

import os

from fileinput import filename



if __name__=="__main__":

    files=os.listdir(".")

    for filename in files:

        print filename

        pos=filename.find(".")

        print pos

        if filename[pos+1:]=="html":

            print filename

            newname=filename[:pos+1]+"htm"

            print newname

            os.rename(filename, newname)

    

例10:テキスト内でhelloを検索する文字列の個数
#-*-coding:utf-8-*-

import sys

import os

import re



if __name__=="__main__":

    fl=file("aaa.txt","r")

    count=0

    for s in fl.readlines():

        li=re.findall("hello", s)   # li       

        if len(li)>0:

            count=count+li.count("hello")

    print "   "+str(count)+" hello"   



    fl.close()