0

Traceback (most recent call last):
File "...\Hello Python\src\main\__init__.py", line 7, in <module>
redBench.accomodate(harold)
TypeError: unbound method accomodate() must be called with bench instance as first argument (got man instance instead)

Here is the code:

__init__py

import man_cl
import bench_cl

harold = man_cl.man("harold")
redBench = bench_cl.bench
[B]redBench.accomodate(harold)[/B]

man_cl.py

class man:
    def __init__(self, name):
        self.name = name
    def sit(self):
        print " is now sitting on bench"

bench_cl.py

class bench:
    occupants = []
[B]    def accomodate(self,person):
        self.occupants.append(person)
        print person.name + " now accomodated"[/B]
    def listOccupants(self):
        for i in range(1, occupants.__len__()):
            print "occupant number " + i + ": " + occupant[i].name

Any ideas?

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by iLikeSunshine
0

For testing purposes I would write it like this ...

class Man(object):
    def __init__(self, name):
        self.name = name
    
    def sit(self):
        print " is now sitting on bench"


class Bench(object):
    def __init__(self):
        self.occupants = []
    
    def accomodate(self, person):
        self.occupants.append(person)
        print person.name + " now accomodated"
    
    def listOccupants(self):
        for i in range(0, len(self.occupants)):
            print "occupant number " + str(i+1) + ": " + self.occupants[i].name


harold = Man("harold")
redBench = Bench()
redBench.accomodate(harold)

larry = Man("larry")
redBench.accomodate(larry)

redBench.listOccupants()
0

Vegaseat,

Thanks! I think the error had to do mainly with the fact that I didn't create a constructor for the Bench class.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.