Python Special Syntax 2

a=None

for i in a:
    print(‘zheli de i shi %d‘ % i)

报错: TypeError: ‘NoneType‘ object is not iterable

除非你提供你自己的return语句,每个函数都在结尾暗含有return None语句。

关于DocString:

def testing(x):
    ‘‘‘This is a test function.

This is a great feature.You must learn how to use this.‘‘‘
    print(‘Your input is ‘,x)

print(testing.__doc__)
testing(18)
testing(5)

help(testing)

输出:

This is a test function.


This is a great feature.You must learn how to use this.
(‘Your input is ‘, 18)
(‘Your input is ‘, 5)
Help on function testing in module __main__:


testing(x)
This is a test function.

This is a great feature.You must learn how to use this.

 

Python Special Syntax 2,布布扣,bubuko.com

时间: 2024-10-05 04:58:36

Python Special Syntax 2的相关文章

Python Special Syntax 8: 序列化与反序列化-->华丽丽的叫 pickle(泡菜?!)

直接上代码吧 #-*-coding:utf-8 import os if os.path.exists('d:\\cpickle.data'): os.remove('d:\\cpickle.data') import cPickle as P shoplist=['apple','banana','pear'] P.dump(shoplist,file('d:\\cpickle.data','w')) f=file('d:\\cpickle.data') while True: content

Python Special Syntax 4:序列相关

元组通过圆括号中用逗号分割的项目定义.元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变. 含有0个或1个项目的元组.一个空的元组由一对空的圆括号组成,如myempty = ().然而,含有单个元素的元组就不那么简单了.你必须在第一个(唯一一个)项目后跟一个逗号,这样Python才能区分元组和表达式中一个带圆括号的对象.即如果你想要的是一个包含项目2的元组的时候,你应该指明singleton = (2 , ). 序列的切片功能: name = 'swaroo

Python Special Syntax 7:继承

继续在上一节的类的基础上测试继承: #-*-coding:utf-8 import Syntax2 class Student(Syntax2.Person): def __init__(self,name,age): Syntax2.Person.__init__(self,name) Syntax2.Person.printName(self) print('age %s' % age) self.age=age def detail(self): print('Name %s, age:%

Python Special Syntax 6:模块的__name__

#-*-coding:utf-8 class Person: __privateName=None; count=0;#既可以被实例对象引用,也可以被当做静态对象引用,NND,好混乱. def __init__(self,name): self.count+=1 Person.count+=2 print('%s has been created' % name) self.__privateName=name; self.name=name def printPrivateName(self)

Python Special Syntax 5:对象相关

__init__方法一样,还有一个特殊的方法__del__,它在对象消逝的时候被调用.但是很难保证这个方法究竟在 什么时候 运行.如果你想要指明它的运行,你就得使用del语句, #-*-coding:utf-8 class Person: __privateName=None;#私有变量 count=0;#既可以被实例对象引用,也可以被当做静态对象引用,NND,好混乱. def __init__(self,name): self.count+=1 Person.count+=2 print('%

Python Special Syntax 12: 伟大的动态执行

exec语句用来执行动态的Python语句 exec 'print "Hello World"'输出: Hello World eval用来计算表达式: eval('2*3') 结果:6 Python Special Syntax 12: 伟大的动态执行,布布扣,bubuko.com

Python Special Syntax 10:列表综合,不定数参数传递(C#的params关键字)

列表综合 参见:http://woodpecker.org.cn/abyteofpython_cn/chinese/ch15s03.html #-*-coding:utf-8 def ttt(value1,*args): for i in args: value1+=i print(value1) def tt2(**args): iSum=args['a']+args['b'] print iSum ttt(10,1,2,3,4) tt2(a=3,b=4) 输出:207 Python Spec

Python Special Syntax 9 : 异常捕获

#-*-coding:utf-8 import sys try: strInput=int(raw_input('input sth.')) f=file('d:\\kjdsklslkdkl2l23223.datdk') except (EOFError,ValueError): #多重异常 print(ValueError.message) print('eof or value') except: print('unknow error') else: print('good') Pytho

Python Special Syntax 11: lambda表达式

lambdTesting=lambda x: x*3 print(lambdTesting(5)) 输出 15 像C#一样传递lambda表达式: def abc(t,a): return t(a) print(abc(lambdTesting,15)) 输出:45 Python Special Syntax 11: lambda表达式,布布扣,bubuko.com