PythonÈëÃŵÄ36¸öÀý×Ó Ö® 32
Ô´´úÂëÏÂÔØ£ºÏÂÔØµØÖ·ÔÚÕâÀï
A Byte Of Python
ÖйØÓڼ̳ÐÓÐÕâÑùµÄÎÄ×Ö£º
Suppose you want to write a program which has to keep track of the
teachers and students in a college. They have some common
characteristics such as name, age and address. They also have specific
characteristics such as salary, courses and leaves for teachers and,
marks and fees for students.
You can create two independent classes for each type and process them
but adding a new common characteristic would mean adding to both of
these independent classes. This quickly becomes unwieldy.
A better way would be to create a common class called SchoolMember and
then have the teacher and student classes inherit from this class i.e.
they will become sub-types of this type (class) and then we can add
specific characteristics to these sub-types.
Õâ¶ÎÎÄ×ֺܼòµ¥£¬ÎÒ¿ªÊ¼Ò²ÕâôÈÏΪ¡£µ«µ±ÎÒ²»Ð¡Ðĵڶþ´Î¶ÁÕâ±¾ÊéµÄʱºò²ÅÁì»áµ½ÕâЩÎÄ×ÖËùÒª´«´ïµÄÉîÒâ¡£
¼Ì³ÐÔÚÎÒ¿´À´£¬»òÕß˵ÔÚÒÔǰµÄÎÒ¿´À´£¬ÊÇΪÁË´úÂëÖØÓã¬Êǵģ¬ÎÒÏÖÔÚÒ²ÕâôÈÏΪ¡£µ«£¬¶ÔÓÚΪʲô¼Ì³ÐÓÐÀûÓÚ´úÂëÖØÓÃÎÒÈ´ÓÐÁ˲»Í¬µÄÀí½â¡£ÒÔǰµÄÀí½â
ÊÇÕâÑùµÄ£ºÎÒÏÈдһ¸öÀ࣬ÒÔºóÐèÒªÀ©Õ¹Õâ¸öÀàµÄ¹¦ÄܵÄʱºò¾ÍÏȼ̳ÐËü£¬È»ºóÔÙÌí¼ÓһЩ·½·¨µÈ¡£ÕâÑù¾ÍËãÊÇ´úÂëÖØÓÃÁË¡£ÏÖÔÚÎÒÓÐÁËеÄÀí½â£º¼Ì³ÐµÄ±¾ÖÊÊǽ«¹²ÐԺ͸öÐÔ·ÖÀë¡£
ÔÚÉè¼ÆÀàµÄÒ»¿ªÊ¼£¬ÎÒÃÇÉõÖÁÒѾÃ÷°×ÁËÕâ¸öÀàÔÚÒÔºóµÄÓô¦ÒÔ¼°»á±»ÈçºÎµØ¼Ì³Ð£¬ÎÒÃǽ«¹²ÐԺ͸öÐÔ·Ö¿ª£¬ÊÇÎÒÃǶÔÕû¸öϵͳµÄÐ޸ıäµÃ¼òµ¥¡£ËµµÄ¸üÃ÷°×Ò»µã£¬ÓëÆä˵“¼Ì³Ð»úÖÆ”ÊÇΪÁË´úÂëÖØÓ㬲»Èç˵ÊÇΪÁË´úÂëά»¤¡£ÔÚ¼ÆËã»úÊÀ½ç£¬ÎÞÊýÊÂʵ¸æËßÎÒÃÇ£¬·ÖÀëµÄ¶«Î÷ÊǺõġ£
# 036
class SchoolMember:
def __init__(self, name, age):
self.name = name
self.age = age
print '³õʼ»¯%s' % self.name
# end of def
def tell(self):
print 'Ãû×Ö£º%s ÄêÁ䣺%s' % (self.name, self.age)
# end of def
# end of class
class Teacher(SchoolMember):
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age) # Python²»»á×Ô¼ºµ÷Óûù±¾ÀàµÄ¹¹Ô캯Êý£¬ÎÒÃǵÃ×Ô¼ºµ÷Óá£
self.salary = salary
print '
Ïà¹ØÎĵµ£º
# 015
# ĬÈϲÎÊýµÄ±¾ÖÊÊÇ:
# ²»ÂÛÄãÊÇ·ñÌṩ¸øÎÒÕâ¸ö²ÎÊý,ÎÒ¶¼ÊÇÒªÓÃËü,
# ÄÇôÎÒ¾ÍÒªÏëºÃÔÚÄã²»ÏòÎÒÌṩ²ÎÊýµÄʱºòÎÒ¸ÃʹÓÃʲô¡£
# »òÕߣ¬ÕâÑùÀ´Àí½â£º
# ÓÐһЩ²ÎÊýÔÚÒ»°ãÇé¿öÏÂÊÇÔ¼¶¨Ë׳ɵģ¬
# µ«£¬ÔÚ¼«ÉÙÇé¿öÏ»áÓÐһЩºÜÓиöÐÔµÄÈË»á´òÆÆ´«Í³¶ø°´ÕÕ×Ô¼ºµÄϰ¹ßÀ´×öÊÂ
def theFirstDayInAWeek(theDay = 'Sunda ......
# 017
def lifeIsAMirror():
string = raw_input()
if string == 'I love you!':
return 'I love you, too!'
elif string == 'Fuck you!':
return ''
else:
return
# end of def
string = lifeIsAMirror()
if len(string) == 0:
print 'You have nothing.'
else: ......
¡¡¡¡ÓÐʱºò£¬Òª°ÑÄÚ´æÖеÄÒ»¸ö¶ÔÏó³Ö¾Ã»¯±£´æµ½´ÅÅÌÉÏ£¬»òÕßÐòÁл¯³É¶þ½øÖÆÁ÷ͨ¹ýÍøÂç·¢Ë͵½Ô¶³ÌÖ÷»úÉÏ¡£PythonÖÐÓкܶàÄ£¿éÌṩÁËÐòÁл¯Óë·´ÐòÁл¯µÄ¹¦ÄÜ£¬È磺marshal, pickle, cPickleµÈµÈ¡£½ñÌì¾Í½²½²marshalÄ£¿é¡£
¡¡¡¡×¢Ò⣺
marshal²¢²»ÊÇÒ»¸öͨÓõÄÄ£¿é£¬ÔÚijЩʱºòËüÊÇÒ»¸ö²»±»ÍƼöʹÓõÄÄ£¿é£¬ÒòΪʹÓÃmarshalÐ ......
Ô´´úÂëÏÂÔØ£ºÏÂÔØµØÖ·ÔÚÕâÀï
# 022
listNum1 = [1, 3]
listNum2 = [2, 4]
listStr1 = ['a', 'c']
listStr2 = ['b', 'd']
# ÁбíµÄºÏ²¢
list1 = listNum1 + listStr1
for ele in list1:
print ele
print '\n'
# ÅжÏÁбíÖÐÊÇ·ñ°üº¬Ä³ÔªËØ
print 'b' in list1
print 1 in list1
# ɾ³ýij¸öÔªËØ
for ele in ......
Ô´´úÂëÏÂÔØ£ºÏÂÔØµØÖ·ÔÚÕâÀï
# 023
# Tuple(ÔªËØ×é)ÊDz»¿É±äµÄÁбí
tuple1 = (1, 2, 3)
print tuple1
tuple2 = (tuple1, 4, 5, 6) # Ò»¸öÔªËØ×é¿ÉÒÔ×÷ΪÁíÍâÒ»¸öÔªËØ×éµÄÔªËØ
print tuple2 # ²¢ÇÒÄܹ»ÔÚ´æ´¢µÄʱºò±£³ÖÔʼµÄÂß¼¹ØÏµ
for ele in tuple2:
print ele
print '\n'
for ele in tuple2[0]:
pr ......