0

I have the following code:

from suchandsuch import bot

class LaLaLa():
    def __init__(self):
        self.donenow = 0
        print "LaLaLa() initialized."
        return

    def start(self):
        pages = bot.cats_recursive('something')
        for page in pages:
            self.process_page(page)

When I try to run y = LaLaLa() and then y.start(), though, I get an error:

AttributeError: LaLaLa instance has no attribute 'cats_recursive'

This makes me suspect that Python is trying to call cats_recursive() not from suchandsuch's bot sub-module (as is defined at the beginning of the file), but rather from LaLaLa(), which of course doesn't have the cats_recursive() function. Is there a way to force a class instance to use an imported module, rather than just look inside itself?

1
  • There shouldn't be any problem with this code. Your real code probably does something different... Can you post it ? Commented May 19, 2013 at 16:59

3 Answers 3

1

Posters are correct that there is nothing wrong with the code you have posted.

It's the code you didn't post that is probably the problem. It is hinted at in your naming of cats_recursive. You haven't shown us that perhaps LaLaLa is defined in or imported into bot.py.

One way to replicate your error is:

# in suchandsuch/bot.py

class LaLaLa():
    def __init__(self):
        self.donenow = 0
        print "LaLaLa() initialized."
        #  don't need a `return` here

def start(self):
    pages = bot.cats_recursive('something')
    for page in pages:
        self.process_page(page)

bot = LaLaLa()

That's just one. Another is to have __init__.py in such and such something like:

bot = LaLaLa()

Like I said, the error is in your code structure.

print the id of the bot inside LaLaLa or captrue the error with pydb and I suspect you will see that bot is an instance of LaLaLa other than y (again check the id's)

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

1 Comment

The error was that I had set the instance of LaLaLa() to a variable named bot - exactly as you exemplified! Thanks much.
1

You are doing fine. Most probably there is no cats_recursive() attribute in your module for real. Check syntax, check module content.

1 Comment

Well, the message does refer to a LaLaLa instance. That would mean there's another class named LaLaLa (defined or used) in suchandsuch.
1

You might find the easiest way to do this would be to try to assign the cats_recursive() to the pages variable outside the class and then pass the variable to the start() function as a parameter. If this works then keep it that way, if it doesn't work then there's probably something wrong with the code elsewhere.

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.