2010-09-17 45 views
0

如果要打印一堆Python中的变量,你有好几个选择,如:Python中的raise对象在哪里?

for i in range(len(iterable)): 
    print iterable[i].name 

OR

map(lambda i: sys.stdout.write(i.name), iterable) 

我之所以用它代替sys.stdout.write函数在第二个例子中打印的是lambda表达式不会接受打印,但是sys.stdout.write用于同样的目的。

您也可以有条件地与三元运算符印:

map(lambda n: None if n % 2 else sys.stdout.write(str(n)), range(1, 100)) 

所以这将是非常方便的,如果我可以检查条件的整个序列,将准予例外以这样的方式:

map(lambda o: raise InvalidObjectError, o.name if not o.isValid else o(), iterable) 

但这并不奏效。 在Python中有没有这样的提升对象,如果有,它在哪里?

+1

-1:哇,这一切都很糟糕。请不要玩这样的代码高尔夫球。请不要让Python无法读取。第一种风格有什么问题?这是有道理的。为什么使用让事物变得模糊的奇怪的地图技术?这些默默无闻的重点是什么?它不会更快。阅读起来并不容易。为什么要这样做? – 2010-09-17 10:05:14

+0

@S。洛特前两个地图的例子是无用的,但他们只是演示sys.stoud.write和我想用raise的方式。但是我不明白为什么第三张地图可能会坏。 – cory 2010-09-17 15:53:34

+0

为什么在使用'from __future__ import print_function'时使用'sys.stdout.write'? – 2010-09-17 17:28:58

回答

4

没有Python的“对象”(内置或标准库)为raise,你必须建立一个自己(典型的短片断进去一个人的util.py ...!):

def do_raise(exc): raise exc 

通常被称为do_raise(InvalidObjectError(o.name))

+0

我对名字'do_raise'感到c 0123' – Gabe 2010-09-17 03:56:58

+2

使用Python关键词作为标识符的传统方法是'raise_'。 – 2010-09-17 04:14:19

+0

@加贝:或许你应该对这个被接受的答案感到不安。 – 2010-09-17 10:22:03

2

我不认为有可能在lambda中使用raise,就像您正在尝试的那样。 raise是一个语句/表达式,而不是一个对象。正如@Alex Martelli所说的,您可能需要定义一个函数来为您进行检查。现在,该函数可以在同一个上下文中进行本地声明。

至于异常类型,这是你的问题似乎针对:异常类型不会自动定义。对于简单的异常类型,你要么需要只是一个文本消息,或者根本没有,典型的异常类型是在你的模块/文件范围为简单的定义:

class InvalidObjectError(Exception): pass 
+0

其实你的回答的第一部分是正确的,我确实想知道是否有一个用于加载语句的对象,就像打印语句的对象一样。 – cory 2010-09-17 03:00:58

+0

“我不认为有可能在lambda中使用raise,就像你试图做的那样。raise是一个语句/表达式,而不是一个对象。”有人可能会说这意味着python的功能不够。 – g33kz0r 2012-05-14 04:11:03

0

对于你的第一个例子中,我使用的形式是这样的:

print '\n'.join(obj.name for obj in iterable) 

而且我会用:

firstnotvalid = next(obj.name for obj in iterable if not obj.is_valid()) 

,而不是和:

>>> import sys 
>>> map(lambda n: None if n % 2 else sys.stdout.write(str(n)), range(1, 100)) 
2468101214161820222426283032343638404244464850525456586062646668707274767880828486889092949698[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None] 

我会做:

>>> print (', '.join(str(number) for number in range(1,100) if not number % 2)) 
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98 

忽略不存在的一系列步骤参数,因为我认为功能是其他更复杂的功能简化。

1

Do.不。做。这个。

这是一个可怕的想法。

map(lambda o: raise InvalidObjectError, o.name if not o.isValid else o(), iterable) 

这样做。

class SomeValidatingClass(object): 
    def isValid(self): 
     try: 
      self.validate() 
     except InvalidObjectError: 
      return False 
     return True 
    def validate(self): 
     """raises InvalidObjectErorr if there's a problem.""" 

[ x.validate() for x in iterable ] 

没有地图。没有拉姆达。同样的行为。

+0

地图和lambda有什么问题?在我看来,你还会推荐使用for循环和if语句来代替filter(),它只能用于(在我的眼中)绘制过程。 – cory 2010-09-17 19:22:09

+0

@cory:过滤器很难与此相媲美。使用'lambda'试图引发异常太复杂。没有评论过滤。只评论你的确切用例。这里的实际验证非常简单得多。没有地图。没有Lambda。 '[x.validate()for x in iterable]''。如果有任何对象无效,则会引发异常。完成。没有地图。没有Lambda。 – 2010-09-17 19:58:53