Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

pythonѧϰ1 ʹÓÃÀà

#ʹÓÃÀà
class CPerson:
#Àà±äÁ¿ºÃ±ÈC++Öеľ²Ì¬³ÉÔ±±äÁ¿
population = 0

def SayHi(self):
print('Hello World')
def HowMany(self):
if CPerson.population == 1:
print('I am the only person here.')
else:
print(('We have %d persons here.') % CPerson.population)

#ÀàÖÐÓкܷ½·¨µÄÃû×ÖÓÐÌØÊâµÄÒâÒå
#__init__ºÃ±ÈC++ÖеĹ¹Ô캯Êý
def __init__(self, name):
self.name = name
print(('Initializing %s') % self.name)
CPerson.population += 1

#__del__ºÃ±ÈC++ÖеÄÎö¹¹º¯Êý
def __del__(self):
CPerson.population -= 1
if CPerson.population == 0:
print('I am the last one.')
else:
print(('There are still %d people left.') % CPerson.population)

p = CPerson('123456')
p.SayHi()
p.HowMany()
p0 = CPerson('987654321')
p0.SayHi()
p0.HowMany()
p.SayHi()
p.HowMany()
print('------------------------------------------')
#ʹÓü̳У¬¶à̬ÏÖÏó
class SchoolMember:
def __init__(self, name ,age):
self.name = name
self.age = age
print(('Initialized SchoolMember:%s') % self.name)
def Tell(self):
print(('Name:%s, Age:%d') % (self.name, self.age))
class Teacher(SchoolMember):
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print(('Initialized Teacher:%s') % self.name)
def Tell(self):
SchoolMember.Tell(self)
print(('Salary:%d') % self.salary)
class Student(SchoolMember):
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print(('Initalized Student:%s') % self.name)
def Tell(self):
SchoolMember.Tell(self)
print(('Marks:%d') % self.marks)
t = Teacher('liyong', 30, 30000)
s = Student('swap', 22, 75)
members = [t, s]
for member in members:
print('######


Ïà¹ØÎĵµ£º

Python Ïß³Ìͬ²½¶ÓÁÐ

ÎÒÃǾ­³£»á²ÉÓÃÉú²úÕß/Ïû·ÑÕß¹ØϵµÄÁ½¸öÏß³ÌÀ´´¦ÀíÒ»¸ö¹²Ïí»º³åÇøµÄÊý¾Ý¡£ÀýÈçÒ»¸öÉú²úÕßÏ߳̽ÓÊÜÓû§Êý¾Ý·ÅÈëÒ»¸ö¹²Ïí»º³åÇøÀµÈ´ýÒ»¸öÏû·ÑÕßÏß
³Ì¶ÔÊý¾ÝÈ¡³ö´¦Àí¡£µ«ÊÇÈç¹û»º³åÇøµÄ̫С¶øÉú²úÕߺÍÏû·ÑÕßÁ½¸öÒì²½Ï̵߳ÄËٶȲ»Í¬Ê±£¬ÈÝÒ׳öÏÖÒ»¸öÏ̵߳ȴýÁíÒ»¸öÇé¿ö¡£ÎªÁ˾¡¿ÉÄܵÄËõ¶Ì¹²Ïí×ÊÔ´²¢ÒÔÏàͬ
Ëٶȹ¤×÷µÄ¸÷Ïß³ ......

example of python operator overloadind

And last here is the overload operators example:
# map() takes two (or more) arguments, a function and a list to apply the function to
# lambda can be put anywhere a function is expected
# map() calls lambada for every element in the self list
# since Vector has overloaded __getitem__ and __len_ ......

pythonË㷨ʵ¼ù3 ðÅÝÅÅÐò

#ðÅÝÅÅÐò
def BubbleSort(mylist):
n = len(mylist)
i = 0
j = 0
bExchange = False
for i in range(1, n):
bExchange = False
j = n - 1
while j >= i:
if mylist[j] < mylist[j - 1]:
tmp = mylist[j]
myli ......

pythonË㷨ʵ¼ù6 ¶ÑÅÅÐò

#¶ÑÅÅÐò
def Heapify(mylist, start, end):
left = 0
right = 0
maxv = 0
left = start * 2
right = start * 2 + 1
while left <= end:
maxv = left
if right <= end:
if mylist[left] < mylist[right]:
maxv = right
......

Python ×ÖµäʹÓÃÀý×Ó


#!/usr/bin/env python
#coding=utf-8
def buildConnectionString(params):
    return ":".join(["%s=%s" %(k, v) for k, v in params.items()])
if __name__ == "__main__":
    myParams = {"server":"mpilgrim", \
               &nbs ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ