1

When working in my plugin's main python function, I receive an AttributeError anytime I call a class method from within the class itself.

For example the sample code below:

class PluginName:
    def __init__(self, iface):
        ...
        self.do_something()

    def do_something(self):
        ...

Would result in the error: AttributeError: PluginName instance has no attribute 'do_something'

Shouldn't do_something() be recognized as a method rather than an attribute because of the parentheses?

  • Please share a code snippet that shows how you are using this method so that I can pinpoint what is actually causing this error. – Asad Abbas May 3 '18 at 4:27
2

I was getting the same error for a long time, I am new to this, but in my function when I remove the underscores from the function name it works fine. I figured underscores are treated in some different way in django.

def getname(self):
    return self.name

def getname1(self):
    return self.name

def lastseen(self):
    return cache.get('seen_%s' % (self.user.username))



def last_seen(self):
    return cache.get('seen_%s' % (self.user.username))

The .last_seen() doesn't work, The .lastseen() does, The .getname1() doesn't work, The .getname() does.

To be honest I don't know if anyone does I would love to know why. Thanks

| improve this answer | |
2

This is really late, but in case anyone has this problem again, as I did, and looks here, hopefully this helps them.

Some text editors such as Sublime Text occasionally mess up the tabs, so the spacing you see in the text editor is not necessarily what Python sees. Since tabs are important in Python, this can lead to your do_something function being defined within the init function rather than as a separate function. Hence, when you call self.do_something(), Python will not have created the function yet and it will fail.

To fix this, open the file in another text editor. I find the simplest text editors like 'Text Editor' in Ubuntu, 'Notepad' in Windows, or 'TextEdit' in Mac work best. You will likely see immediately where the spacing went wrong and can fix it there. If it is not immediately clear, try deleting the tabs and redoing them.

This fixed the problem for me. Thanks.

| improve this answer | |
0

The same error happends when also, as an example:

You have a variable in the constructor called "X" and you use it in "do_something" function but u define it after calling the function in the constructor, like:

def __init__(self, iface):
        ...
        self.do_something()
        self.x = 0

def do_something(self):
        self.x = 1 <------- you will get the error here
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.