2006年06月15日 星期四 12:43
从开始接触 python 我就认为这是一门崇尚简洁的语言。 在学习到print 函数的时候有一些疑惑,比如: name="tom" age=20 print "%s is %d old years" % (name, age) 如果改写一下print ,如下就会出错: print "%d is %s old years" % (name, age) 在我们程序员的角度考虑的话,name是字符串类型要按字符串类型打印,age是数值类型要按数值类型打印,如此考虑的话,哪里用%s,哪里用%d就是明确的了。 但是对于解释器来说,谁是字符串,谁是数值,它是最清楚的了,如何还要程序员来指定哪? 这个问题难道仅仅是因为python 沿用了 c 的print吗?
2006年06月15日 星期四 13:06
print name + " is " + str(age) 也行 On 6/15/06, yi feng <yi2369 at gmail.com> wrote: > 从开始接触 python 我就认为这是一门崇尚简洁的语言。 > 在学习到print 函数的时候有一些疑惑,比如: > name="tom" > age=20 > print "%s is %d old years" % (name, age) > > 如果改写一下print ,如下就会出错: > print "%d is %s old years" % (name, age) > > 在我们程序员的角度考虑的话,name是字符串类型要按字符串类型打印,age是数值类型要按数值类型打印,如此考虑的话,哪里用%s,哪里用%d就是明确的了。 > > 但是对于解释器来说,谁是字符串,谁是数值,它是最清楚的了,如何还要程序员来指定哪? > > 这个问题难道仅仅是因为python 沿用了 c 的print吗? > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > >
2006年06月15日 星期四 13:31
On 6/15/06, yi feng <yi2369 at gmail.com> wrote: > 从开始接触 python 我就认为这是一门崇尚简洁的语言。 > 在学习到print 函数的时候有一些疑惑,比如: > name="tom" > age=20 > print "%s is %d old years" % (name, age) > > 如果改写一下print ,如下就会出错: > print "%d is %s old years" % (name, age) > > 在我们程序员的角度考虑的话,name是字符串类型要按字符串类型打印,age是数值类型要按数值类型打印,如此考虑的话,哪里用%s,哪里用%d就是明确的了。 > > 但是对于解释器来说,谁是字符串,谁是数值,它是最清楚的了,如何还要程序员来指定哪? > > 这个问题难道仅仅是因为python 沿用了 c 的print吗? > 因为字符串与数值的表示是不同的。字符串是一个个的ascii码,而数值则是16进制表示。而且数值可以表示为10进制和8进制,16进制等,是不一样的,还有象科学计数等,因此如果没有格式,如何进行控制。 -- I like python! My Blog: http://www.donews.net/limodou My Django Site: http://www.djangocn.org NewEdit Maillist: http://groups.google.com/group/NewEdit
2006年06月15日 星期四 13:37
确切的说,python中并不怎么看重类型,重要的是接口
>>> class foo:
... def __int__(self):
... return 99
... def __str__(self):
... return 'abc'
...
>>> f=foo()
>>> '%s, %d' % (f, f)
'abc, 99'
>>> '%s' % 45
'45'
>>>
>>> class ix(int):
... def __str__(self):
... return hex(self)
...
>>> i=ix(100)
>>> i
100
>>> class sl(str):
... def __int__(self):
... return len(self)
...
>>> s=sl('asdfasdf')
>>> s
'asdfasdf'
>>> '%s, %d' % (i, s)
'0x64, 8'
2006/6/15, yi feng <yi2369 at gmail.com>:
>
> 从开始接触 python 我就认为这是一门崇尚简洁的语言。
> 在学习到print 函数的时候有一些疑惑,比如:
> name="tom"
> age=20
> print "%s is %d old years" % (name, age)
>
> 如果改写一下print ,如下就会出错:
> print "%d is %s old years" % (name, age)
>
>
> 在我们程序员的角度考虑的话,name是字符串类型要按字符串类型打印,age是数值类型要按数值类型打印,如此考虑的话,哪里用%s,哪里用%d就是明确的了。
>
> 但是对于解释器来说,谁是字符串,谁是数值,它是最清楚的了,如何还要程序员来指定哪?
>
> 这个问题难道仅仅是因为python 沿用了 c 的print吗?
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060615/f663587f/attachment.htm
2006年06月15日 星期四 14:43
这么写多好看 : print name,'is',age,'old years' On 6/15/06, yi feng <yi2369 at gmail.com> wrote: > > 从开始接触 python 我就认为这是一门崇尚简洁的语言。 > 在学习到print 函数的时候有一些疑惑,比如: > name="tom" > age=20 > print "%s is %d old years" % (name, age) > > 如果改写一下print ,如下就会出错: > print "%d is %s old years" % (name, age) > > > 在我们程序员的角度考虑的话,name是字符串类型要按字符串类型打印,age是数值类型要按数值类型打印,如此考虑的话,哪里用%s,哪里用%d就是明确的了。 > > 但是对于解释器来说,谁是字符串,谁是数值,它是最清楚的了,如何还要程序员来指定哪? > > 这个问题难道仅仅是因为python 沿用了 c 的print吗? > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- 考研还是工作 哎... ! http://codeplayer.blogbus.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060615/3f3ef215/attachment.htm
2006年06月15日 星期四 15:12
用格式化字符串或者模板的方法感觉更灵活一些,方便把一些字符串常量和格式信息集中配置。 On 6/15/06, yi huang <yi.codeplayer at gmail.com> wrote: > > 这么写多好看 : > print name,'is',age,'old years' > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060615/9c25f512/attachment.htm
2006年06月15日 星期四 15:48
yi feng wrote:
> 从开始接触 python 我就认为这是一门崇尚简洁的语言。
> 在学习到print 函数的时候有一些疑惑,比如:
> name="tom"
> age=20
> print "%s is %d old years" % (name, age)
>
> 如果改写一下print ,如下就会出错:
> print "%d is %s old years" % (name, age)
>
> 在我们程序员的角度考虑的话,name是字符串类型要按字符串类型打印,age是
> 数值类型要按数值类型打印,如此考虑的话,哪里用%s,哪里用%d就是明确的了。
>
> 但是对于解释器来说,谁是字符串,谁是数值,它是最清楚的了,如何还要程序
> 员来指定哪?
>
> 这个问题难道仅仅是因为python 沿用了 c 的print吗?
看看这些例子, 自己揣摩一下 :)
>>> "%1.3d" % 5
'005'
>>> "%3d" % 5
' 5'
>>> "%s" % 5
'5'
class A:
def __str__(self):
return "str(me)"
def __repr__(self):
return "repr(me)"
a = A()
>>> "I am : %s" % a
"I am : str(me)"
另外,
1. print是语句, 不是函数
2. print后面那部分不是print语句的一部分, 而是一个表达式, 可以脱离print使
用.例如,
a_str = "hello %s" % "world"
2006年06月15日 星期四 16:30
On 6/15/06, jacob <jacob at exoweb.net> wrote: > > yi feng wrote: > > 另外, > 1. print是语句, 不是函数 > 2. print后面那部分不是print语句的一部分, 而是一个表达式, 可以脱离print使 > 用.例如, > a_str = "hello %s" % "world" 更确切的说,print后面用逗号分隔的每个部分都是一个表达式,print相当于调用每个表达式结果的__repr__方法然后再用空格连接起来。 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060615/78829146/attachment.html
2006年06月15日 星期四 17:40
> > > 更确切的说,print后面用逗号分隔的每个部分都是一个表达式,print相当于调用每个表达式结果的__repr__方法然后再用空格连接起来。 > print 使用的 各个对象的 __str__ 方法 -- 开飞机的舒克 http://www.lvye.org/shuke msn:weizhong at netease.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060615/8028cbb7/attachment.htm
2006年06月15日 星期四 17:43
各位好! 有谁知道如何控制命令行控制台的输出,例如打印忙信息(就是一个不断转动的“|“);清屏,等等。 谢谢。 print "Hello World" # 直接输出,有回行符 print "Hello", # 无回行符输出 print "World" -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060615/1e646195/attachment.html
2006年06月15日 星期四 17:50
nod。 >>> class C: ... def __str__(self): ... return "This is __str__" ... def __repr__(self): ... return "This is __repr__" ... >>> c = C() >>> print c This is __str__ >>> 在06-6-15,魏忠 <weizhong2004 at gmail.com> 写道: > > > 更确切的说,print后面用逗号分隔的每个部分都是一个表达式,print相当于调用每个表达式结果的__repr__方法然后再用空格连接起来。 > > > > > print 使用的 各个对象的 __str__ 方法 > -- I like Python & Linux. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060615/36afd27f/attachment.htm
2006年06月15日 星期四 18:15
记错了...多谢纠正。 On 6/15/06, 员旭鹏 <recordus at gmail.com> wrote: > > nod。 > > >>> class C: > ... def __str__(self): > ... return "This is __str__" > ... def __repr__(self): > ... return "This is __repr__" > ... > >>> c = C() > >>> print c > This is __str__ > >>> > > > 在06-6-15,魏忠 <weizhong2004 at gmail.com> 写道: > > > > > 更确切的说,print后面用逗号分隔的每个部分都是一个表达式,print相当于调用每个表达式结果的__repr__方法然后再用空格连接起来。 > > > > > > > > print 使用的 各个对象的 __str__ 方法 > > > > -- > I like Python & Linux. > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060615/00ebbc3b/attachment.htm
2006年06月15日 星期四 20:01
多谢各位的解答! 我厘清了这些概念,请大家指点: 1.print 是个命令语句,不是函数; 2."%s" % "name";"%d" % 10 都是表达式; 3.因为表达式与print 命令是相对独立的,并且有自己的含义,所以%s 、%d 为了print 而统一是不可能的。
2006年06月15日 星期四 21:53
import time
import sys
for i in range(10):
sys.stdout.write("|")
time.sleep(0.3)
sys.stdout.write(chr(13))
sys.stdout.write("\\")
time.sleep(0.3)
sys.stdout.write(chr(13))
sys.stdout.write("-")
time.sleep(0.3)
sys.stdout.write(chr(13))
sys.stdout.write("/")
time.sleep(0.3)
sys.stdout.write(chr(13))
# end
Best regards,
Alec Jang
Steven Wang (:) wrote:
>
> 各位好!
>
> *有谁知道如何控制命令行控制台的输出,例如打印忙信息(就是一个不断转动的“|
> “);清屏,等等。*
> * *
> * 谢谢。 *
> * *
> print "Hello World" # 直接输出,有回行符
> print "Hello", # 无回行符输出
> print "World"
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
2006年06月16日 星期五 10:37
在06-6-15,Steven Wang (:) <zdwang at xinces.com> 写道: > > 各位好! > > *有谁知道如何控制命令行控制台的输出,例如打印忙信息(就是一个不断转动的"|");清屏,等等。* > ** > *import* time *import* sys chars = "*|/-**\\*" *for* i *in* xrange(40): sys.stdout.write(chars[i%4]) sys.stdout.flush() time.sleep(0.1) sys.stdout.write("*\b*") -- I like Python & Linux. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060616/ab30c5b1/attachment.html
Zeuux © 2025
京ICP备05028076号