2

I'm very much new to Python, essentially being pushed into a new project with no knowledge of the language whatsoever. I've gone over a number of tutorials to get a gist of the syntax and some of the functions, but I'm currently stumped on something that seems pretty basic.

I have a class GeoLocationHandlerObj in GeoLocationSolver.py which has a method '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)

I import it and try to call 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()

When I run this code, I get the following error:

AttributeError: 'GeoLocationHandlerObj' object has no attribute 'myMethod'.

What am I missing in order to successfully call this method?

15
  • If you are learning, please learn python3 Commented Jul 18, 2018 at 8:30
  • What do you get from print(dir(handler))? Commented Jul 18, 2018 at 8:34
  • Why are you using the return(None) in the init function? and try using a different name than handler.. you might be importing that from one the libraries Commented Jul 18, 2018 at 8:50
  • 1
    import * clutters the namespace and you don't necessarily know with what. With my suggestion, you would then reference the class with connector = gls.GeoPSConnectorObj(...) then you're explicit which module that class comes from. Commented Jul 18, 2018 at 9:07
  • 1
    using import GeoPackage.GeoCoding.GeoLocationSolver as gls, print(gls.__file__) is the path correct? Commented Jul 18, 2018 at 10:33

1 Answer 1

2

I had the same problem, adding two new methods to a previously existing class, trying to call the methods would fail because of "object has no attribute". Finally chased it down to the previous dev using a different tabs/spaces configuration from my editor. Once I changed my new code to use the same configuration as the previous code, Python was able to reference the functions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.