<
div id=”content” contentScore=”625″>粗略看了下a byte of python,总结一下大致与java不同的地方:
1、没有{},全部使用:和缩进代替
2、注释使用#
3、doc使用”’或者”””
4、变量类型比较简单,类似于js,不过定义更像vb或者groovy
方法使用def来定义,只有整形,长整形,复数,浮点4种类型的数
5、比java更是一切都是对象,连int也是
6、输出更像是c
7、数组,list,map比较像js
8、有 this,改叫self了,而且还必须手动传,必须在第一个。。。
9、构造叫init,回收叫del
10、继承使用(),需要手动地调用父类的构造
随便放段code
<
pre>
<
div>
02 |
'''Represents a person.''' |
04 |
def __init__( self , name): |
05 |
'''Initializes the person's data.''' |
07 |
print '(Initializing %s)' % self .name |
08 |
# When this person is created, he/she |
09 |
# adds to the population |
10 |
Person.population + = 1 |
14 |
print '%s says bye.' % self .name |
15 |
Person.population - = 1 |
16 |
if Person.population = = 0 : |
17 |
print 'I am the last one.' |
19 |
print 'There are still %d people left.' % Person.population |
22 |
'''Greeting by the person. |
23 |
Really, that's all it does.''' |
24 |
print 'Hi, my name is %s.' % self .name |
27 |
'''Prints the current population.''' |
28 |
if Person.population = = 1 : |
29 |
print 'I am the only person here.' |
31 |
About The Author
bjmayor
程序员,码农,php,python,ios,android,go,产品经理,创业。
|