相关疑难解决方法(0)

Python:__ builtin__和__builtins__之间有什么区别?

我今天在编码并发现了一些东西.如果我打开一个新的解释器会话(IDLE)并检查使用该dir函数定义的内容,我会得到:

$ python
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs', 'all', 'any', 'apply', 'basestring', 'bin', 'bool', 'buffer', …
Run Code Online (Sandbox Code Playgroud)

python language-design python-module python-2.7 python-3.x

69
推荐指数
3
解决办法
2万
查看次数

在单元测试中模拟打开(file_name)

我有一个源代码打开一个csv文件并设置一个标头值关联.源代码如下:

def ParseCsvFile(source): 
  """Parse the csv file. 
  Args: 
    source: file to be parsed

  Returns: the list of dictionary entities; each dictionary contains
             attribute to value mapping or its equivalent. 
  """ 
  global rack_file 
  rack_type_file = None 
  try: 
    rack_file = source 
    rack_type_file = open(rack_file)  # Need to mock this line.
    headers = rack_type_file.readline().split(',') 
    length = len(headers) 
    reader = csv.reader(rack_type_file, delimiter=',') 
    attributes_list=[] # list of dictionaries. 
    for line in reader: 
      # More process to happeng. Converting the rack name to sequence. 
      attributes_list.append(dict((headers[i],
                                   line[i]) …
Run Code Online (Sandbox Code Playgroud)

python unit-testing mox

24
推荐指数
4
解决办法
3万
查看次数

Python:如何为关键字"属性"取消阴影?

我支持一个遗留的 python应用程序,它有一个这样编写的类(仍然在python 2.4中运行):

class MyClass(object):

    def property(self, property_code, default):
        ...
Run Code Online (Sandbox Code Playgroud)

现在我要添加一些新代码:

    def _check_ok(self):
        ...

    ok = property(lamdba self:self._check_ok())
Run Code Online (Sandbox Code Playgroud)

基本上我想为这个类添加一个属性'ok'.

但它不起作用.我遇到此错误消息:

TypeError: property() takes at least 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)

现有的类方法'property'掩盖了内置的'property'关键字.

有没有什么方法可以像我的新代码一样使用'property'

重构现有property()功能不是一种选择.

编辑:如果我把新代码放在MyClass::propertydef 之前,它会工作.但我真的想看看是否有更好的解决方案

编辑2:这些代码在shell中工作

>>> class Jack(object):
...   def property(self, a, b, c):
...      return 2
...   p = __builtins__.property(lambda self: 1)
...
>>> a = Jack()
>>> a.p
1
>>> a.property(1, 2, 3)
2
Run Code Online (Sandbox Code Playgroud)

但是同样的技术在我的应用程序中不起作用.Got AttributeError:'dict'对象没有属性'property'错误

python

3
推荐指数
1
解决办法
920
查看次数