3

So in the interest of practicing Python and more specifically understanding object oriented programing I have written this simple script to better understand the concepts. However when I try to initiate a "monkeys" object what ends up happening is Python adds the name of my first monkey object indefinitely... Am I approaching OOP right? And if so, where am I going wrong, because I can't tell... Thanks

#! usr/bin/python
monkeylist = []
class monkey:
    def __init__(self, name):
       self.name = name
       self.banana = []
               monkeylist.append(self.name)
    def addbanana(self, kind):
       self.banana.append(kind)

class monkeys:
    def __init__(self, monkeylist):
        self.allmonkeys = monkeylist
        self.monkeydict = {}
        for name in self.allmonkeys:
            self.allmonkeys[name] = monkey(name)
    def addbanana(self, name, kind):
       self.monkeydict[name].addbanana(kind)

The exact input and output is this...

python -i objtest.py
>>> bob = monkey("bob")
>>> test = monkeys(monkeylist)
^CTraceback (most recent call last):
File "<stdin>", line 1, in <module>
File "objtest.py", line 15, in __init__
self.allmonkeys[name] = monkey(name)
File "objtest.py", line 7, in __init__
monkeylist.append(self.name)
KeyboardInterrupt
4
  • 1
    I cannot spot an error. Can you please show the full code, including your output? Commented Apr 20, 2011 at 23:01
  • This seems to work for me ? I'm doing m=monkeys(monkeylist);print type(m) , which correctly returns "type 'instance'". As Achim says, can you post the rest of the code and show the output. Commented Apr 20, 2011 at 23:03
  • 2
    Two minor notes for the future: (1) Class names should be in CamelCase (i.e. Monkey and Monkeys) and variable/member/function/method names should be lowercase_with_underscores for the sake of convention. (2) Classes should derive from object (class Monkey(object)) in Python 2.x, as "old-style classes" are just cruft and headaches. Commented Apr 20, 2011 at 23:07
  • you seem to have added a monkeylist.append(self.name) in your init. Its indented wrong. Only indent for block level. Also its probably not the best idea if you are iterating throough it in the Monkeys class to alter it in the Monkey class IMHO. Commented Apr 20, 2011 at 23:50

3 Answers 3

3

Assuming you initialise a Monkeys with a list of names, instead of

self.allmonkeys[name] = monkey(name)

I think you want

self.monkeydict[name] = monkey(name)

FYI the Python naming convention is that classes be uppercased. Also I don't think Monkeys is a descriptive name; Zoo might be better. Note also that monkeylist is a confusing name for a list of names (i.e. not a list of Monkeys).

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

1 Comment

Thanks for the tips, your solution worked. I'm trying to wrap my head around these concepts and appreciate you help.
1

Your code could do with a bit of a clean-up. The monkeys class is redundant, since it merely embodies the dict it contains. You could simply write this:

mm = dict((name, monkey(name)) for name in monkeylist)
mm['harold'].addbanana('green')

OOP is not an end in its own right. Use it when it simplifies things. In this case, using OOP for the monkeys collection created room for a bug to creep in (which is why I thought I'd post this as an answer rather than a comment).

5 Comments

monkey(name) not monkey. Also {name: monkey(name) for name in monkeylist} is a nice feature of Py2.7+.
@katrielalex: Thanks for the correction. Also, I prefer to stick with 2.6 syntax, at least for a little while longer.
Well if I were to see the contents of "monkeylist" i find that it has added the name thousands of times.... Also, wouldn't I need to have another class if I wanted to perform methods on multiple "monkeys"?
@Pete: I realised what went wrong and changed my answer (probably while you were writing the above comment). Sorry for the confusion.
Oh, ok I understand. I was merely trying to create some trivial cases where could use some OOP. But nevertheless, point taken.
0

You probably want instead of:

for name in self.allmonkeys:
    self.allmonkeys[name] = monkey(name)

this

for name in self.allmonkeys:
    self.monkeydict[name] = monkey(name)

Or even:

self.monkeydict = dict((name, monkey(name)) for name in allmonkeys) 

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.