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

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 '


Ïà¹ØÎĵµ£º

pythonÔËËã·û ¹©ÖØÔزο¼

¶þÔªÔËËã·û¼°Æä¶ÔÓ¦µÄÌØÊâ·½·¨
¶þÔªÔËËã·û
ÌØÊâ·½·¨
+
__add__,__radd__
-
__sub__,__rsub__
*
__mul__,__rmul__
/
__div__,__rdiv__,__truediv__,__rtruediv__
//
__floordiv__,__rfloordiv__
%
__mod__,__rmod__
**
__pow__,__rpow__
<<
__lshift__,__rlshift__
>>
_ ......

PythonÈëÃŵÄ36¸öÀý×Ó——04 ÓÅÑŵÄ×Ö·û´®

# 004
# ÀûÓÃÈýÒýºÅ(''' or """)¿ÉÒÔָʾ¶àÐÐ×Ö·û´®
print '''line1
line2
line3'''
# ÁíÍ⣬Ä㻹¿ÉÒÔÔÚÈýÒýºÅÖÐÈÎÒâʹÓõ¥ÒýºÅºÍË«ÒýºÅ
print ''' "What's up? ," he replied.'''
# ·ñÔò£¬ÄãºÃʹÓÃתÒå·ûÀ´ÊµÏÖͬÑùµÄЧ¹û
# »¹ÊÇʹÓÃÈýÒýºÅºÃ£¬²»È»¾ÍÆÆ»µÁËÊÓ¾õÃÀÁË
print ' \"Wha ......

PythonÄ£¿éѧϰ

¡¡¡¡ÓÐʱºò£¬Òª°ÑÄÚ´æÖеÄÒ»¸ö¶ÔÏó³Ö¾Ã»¯±£´æµ½´ÅÅÌÉÏ£¬»òÕßÐòÁл¯³É¶þ½øÖÆÁ÷ͨ¹ýÍøÂç·¢Ë͵½Ô¶³ÌÖ÷»úÉÏ¡£PythonÖÐÓкܶàÄ£¿éÌṩÁËÐòÁл¯Óë·´ÐòÁл¯µÄ¹¦ÄÜ£¬È磺marshal, pickle, cPickleµÈµÈ¡£½ñÌì¾Í½²½²marshalÄ£¿é¡£
¡¡¡¡×¢Ò⣺
marshal²¢²»ÊÇÒ»¸öͨÓõÄÄ£¿é£¬ÔÚijЩʱºòËüÊÇÒ»¸ö²»±»ÍƼöʹÓõÄÄ£¿é£¬ÒòΪʹÓÃmarshalÐ ......

PythonÈëÃŵÄ36¸öÀý×Ó Ö® 18

Àý1£º
# _018
# This is a module (if write Chinese in a module, there will be a error)
def func1():
print 'This is function 1.'
def func2():
print 'This is function 2.'
def func3():
print 'This is function 3.'
# 019
# ʹÓÓimport”Óï¾äµ÷ÓÃÄ£¿é£º
import _018_Module
_ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ