为什么我会变得神秘“TypeError:只能用String或number更新值”?

时间:2014-02-25 21:47:29

标签: pyephem

这真是奇怪。如果我运行此代码(作为鼻子测试),它会打印“-0:34:00.0”,一切都很好

def test_o1(self):
    observer = ephem.Observer()
    observer.lon, observer.lat = math.radians(73.9), math.radians(40.7)
    observer.horizon = '-0:34'
    print observer.horizon

但是,如果我这样做:

def test_o2(self):
    location = UserLocation()
    location.foo()

其中UserLocation是:

# Document is mongoengine.Document
class UserLocation(Document):
    [...]
    def foo(self):
        observer = ephem.Observer()
        observer.lon, observer.lat = math.radians(73.9), math.radians(40.7)
        observer.horizon = '-0:34'

我明白了:

Traceback (most recent call last):
  File "/home/roy/deploy/current/python/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/roy/deploy/current/code/pyza/models/test_user_location.py", line 82, in test_o2
   location.foo()
  File "/home/roy/deploy/current/code/pyza/models/user_location.py", line 134, in foo
    observer.horizon = '-0:34'
TypeError: can only update value with String or number

知道可能会发生什么吗?

1 个答案:

答案 0 :(得分:1)

Arrggghhh。我想到了。我的UserLocation源文件以:

开头

来自将来导入unicode_literals

显然,_libastro坚持使用ascii字符串,而不是unicode。