Python:AttributeError:对象没有属性 'method'

标签 python python-2.7 attributeerror

我对 Python 非常陌生,基本上是在对这门语言一无所知的情况下被插入一个新项目。我已经阅读了许多教程来了解语法和一些功能的要点,但目前我对一些看起来非常基本的东西感到困惑。

我在 GeoLocationSolver.py 中有一个类 GeoLocationHandlerObj,它有一个方法“myMethod”:

class GeoLocationHandlerObj(object):
  def __init__(self, connector, debug=0):
    self._debug = debug
    self.locator = GeoLocationSolverObj(connector)
    return(None)

  def close(self):
    ...
    return(None)

  def getAdr(self, point, lang):
    ...
    return(None)

  def getCom(self, point, lang):
    ...
    return(None)

  def getHmp(self, point, lang):
    ...
    return(None)

  def myMethod(self):
    print "test"
    return(None)

我导入它并尝试调用 myFunction:

import sys
import os
import psycopg2
import string
import json
import socket
import random

from GeoPackage.GeoCoding.GeoLocationSolver import *

if __name__ == "__main__":
  connector = GeoPSConnectorObj(...)
  handler = GeoLocationHandlerObj(connector)
  handler.myMethod()

当我运行此代码时,出现以下错误:

AttributeError:“GeoLocationHandlerObj”对象没有属性“myMethod”。

为了成功调用此方法,我缺少什么?

最佳答案

我遇到了同样的问题,向以前存在的类添加两个新方法,尝试调用这些方法会失败,因为“对象没有属性”。最后使用我的编辑器中不同的制表符/空格配置将其追踪到以前的开发人员。一旦我将新代码更改为使用与以前的代码相同的配置,Python 就能够引用这些函数。

关于Python:AttributeError:对象没有属性 'method',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51397124/

相关文章:

python - 在元素计数不等于 1 的组上过滤 DataFrame

python - pandas 合并两个数据框并汇总单元格值

Python os.walk() 方法

python - 在 Python 中将数字四舍五入到 5 万

python - Types.py 错误、重命名问题、AttributeError

python - 机器学习输入和输出的大小

python - ipython笔记本中的"import matplotlib.pyplot as plt"

python - 如何使用 ElementTree 正确解析 utf-8 xml?

python - 为什么即使导入、拼写和文件位置正确,我仍会收到 AttributeError?

python - AttributeError: 'bool' 对象在导入 SAS 数据集时没有属性 'sum'