初学者python错误-未找到属性
我有这类bgp_路线:初学者python错误-未找到属性,python,class,attributes,Python,Class,Attributes,我有这类bgp_路线: 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() tes
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'
回溯(最近一次呼叫最后一次):
文件“testbgpRoute.py”,第6行,在
testRoute.asPath+='blah'
AttributeError:bgp_路由实例没有属性“asPath”
这个错误的原因是什么?
bgp\u路由的实例化不应该将属性asPath初始化为空字符串吗?它被称为
\uuuuuu init\uuuuu
,与任何其他python魔术方法一样,两边都有两个下划线
顺便说一句,构造函数需要一个
路径
参数。它被称为\uuuu init\uuuu
,与任何其他python魔术方法一样,两边都有两个下划线
顺便说一句,您的构造函数需要一个
路径
参数。您拼错了\uuuu init\uuu
:
def _init_(self, path):
两端都需要两个下划线。由于没有使用正确的名称,Python永远不会调用它,并且永远不会执行self.asPath
属性赋值
注意,该方法需要一个path
参数;在构造实例时,需要指定该参数。由于您的\uuuu init\uuuu
方法会忽略此参数,因此您可能希望删除它:
class bgp_route:
def __init__(self):
self.nextHop = None
self.asPath = ''
self.asPathLength = 0
self.routePrefix = None
您拼错了
\uuuu init\uuuu
:
def _init_(self, path):
两端都需要两个下划线。由于没有使用正确的名称,Python永远不会调用它,并且永远不会执行self.asPath
属性赋值
注意,该方法需要一个path
参数;在构造实例时,需要指定该参数。由于您的\uuuu init\uuuu
方法会忽略此参数,因此您可能希望删除它:
class bgp_route:
def __init__(self):
self.nextHop = None
self.asPath = ''
self.asPathLength = 0
self.routePrefix = None
您需要
\uuuu init\uuuu
而不是\uu init\uu
。您需要而不是\uu init\uu
。可能应该添加对象also@PadraicCunningham不确定在此时引入该概念是否有帮助。可能应该添加对象also@PadraicCunningham不确定在这个关头引入这个概念是否有帮助。