【发布时间】:2014-12-25 18:13:15
【问题描述】:
我有这个类 bgp_route:
class bgp_route:
def _init_(self, path):
self.nextHop = None
self.asPath = ''
self.asPathLength = 0
self.routePrefix = None
但是,当我运行以下测试代码时;
from bgp_route import bgp_route
testRoute = bgp_route()
testRoute.asPath += 'blah'
print testRoute.asPath
我收到以下错误:
Traceback (most recent call last):
File "testbgpRoute.py", line 6, in <module>
testRoute.asPath += 'blah'
AttributeError: bgp_route instance has no attribute 'asPath'
这个错误的原因是什么? bgp_route的实例化不应该将属性asPath初始化为空字符串吗?
【问题讨论】:
-
你需要
__init__而不是_init_。
标签: python class attributes