I am trying to update an occupants list in the class Place, after setting it with:

places["smith"] = Place(
        "smith",
        "This is the Blacksmith's Shop",
        "grimwo",
        ("well", "bakery", "mill"),
        ()
    )

    persons["berrol"] = Person("berrol", "bakery", "baker")

The classes are setup as: Person / Place

class Person(object):
    """ This is the Person object for all people in the village """
    def __init__(self, name, location, title):
        # define initial person values
        self.name = name
        self.location = ""
        self.title = title
        self.setLocation(name, location)

    # setup for how this class prints
    def __str__(self):
        s = self.name, self.title
        return s

    # setup to show the person details
    def show(self):
        print self.name, '\t\t', self.location, '\t\t', self.title

    def setLocation(self, name, location):
        # define how a person changes location
        if self.location == "":
        # call Place.getOccupant to set this location
            place = location
            place = places[location]
            place.getOccupants(name)
        else:
        # call Place.remOccupant to remove existing location then set new one
        # no code in place yet


class Place(object):
    """ This is the Place object for all Places. """
    def __init__(self, name, description, owner, targets, occupants):
        # define initial variables
        self.name = name
        self.description = description
        self.owner = owner
        self.targets = targets
        self.occupants = occupants
        self.status = ""

    # setup to show this place details
    def show(self):
        print self.description
        if self.name != "well":
            if self.owner in self.occupants:
                self.status = "open"
                print "The", self.name, "is open!"
                self.showOccupants()
            else:
                self.status = "closed"
                print "We're sorry the", self.name, "is currently closed."
        else:
            self.showOccupants()

        self.showTargets()

    # setup to show available targets of this place
    def showTargets(self):
        if self.targets:
            print "From here you can go: "
            for target in self.targets:
                print "\t", target
    
    # setup to show the occupants of this place
    def showOccupants(self):
        if self.occupants:
            print "Currently here are: "
            for occupant in self.occupants:
                print "\t", occupant

    def getOccupants(self, occupant):
        # define how to add an occupant
        self.occupants += (occupant, )

    def remOccupants(self, occupant):
        # define how to remove an occupant
        if occupant in self.occupants:
            self.occupants.remove(occupant)

I am successful at setting the inital 'occupant,' but when trying to remove someone so that they can be added to another Place, I am receiving the error: AttributeError: 'str' object has no attribute when trying to use the code:

Change code:

berrol.setLocation(berrol, well)

Any help would be appreciated.

berol is both object instance and parameter to method, which already has self parameter. I would organize location for a object as its property and moving to other location with only one parameter, the destination. Object instances would be occupant values of one place instance. Object would move by giving leave message to old place and arrive message to new one and save new location after success. That is my 5 cents of theoretical knowledge.

berol is both object instance and parameter to method, which already has self parameter. I would organize location for a object as its property and moving to other location with only one parameter, the destination. Object instances would be occupant values of one place instance. Object would move by giving leave message to old place and arrive message to new one and save new location after success. That is my 5 cents of theoretical knowledge.

So, I am using the Person object with a property of location

class Person(object):
    def __init__(self, name, location, title):
        define initial person values
        self.name = name
        self.location = ""
        self.title = title
        self.setLocation(name, location)

However, I have been matching that location to the Occupants list of the Place object

class Place(object):
    """ This is the Place object for all Places. """
    def __init__(self, name, description, owner, targets, occupants):
        define initial variables
        Self.name = name
        self.description = description
        self.owner = owner
        self.targets = targets
        self.occupants = occupants
        self.status = ""

and I have accomplished this (as far as adding new occupants) using:

def getOccupants(self, occupant):
    define how to add an occupant
    self.occupants += (occupant, )

If I only store this information in the Person object, how would I reflect it in the Place object? The theory of using a leave message from an old place and an arrive message on a new one sounds good, in theory, but if I am not using the self.occupants property of the Place, then what?

This I got when I fooled around with your code a little: ( changed getOccupants to addOccupants to reflect the function)

persons = dict()
places=dict()

class Person(object):
    """ This is the Person object for all people in the village """
    def __init__(self, name, location, title):
        # define initial person values
        self.location = ""
        self.name = name
        self.title = title
        self.setLocation(location)

    # setup for how this class prints
    def __str__(self):
        return 'Person( %s, %s )' % (self.name , self.title)

    # setup to show the person details
    def show(self):
        print self.name, '\t', self.location, '\t', self.title

    def setLocation(self, location):
        # define how a person changes location
        if self.location == "":
        # call Place.addOccupant to set this location
            self.location = location
            places[location].addOccupants(self.name)
        else:
        # call Place.remOccupant to remove existing location then set new one
        # no code in place yet
            places[self.location].remOccupants(self.name)
            self.location = location
            places[location].addOccupants(self.name)

class Place(object):
    """ This is the Place object for all Places. """
    def __init__(self, name, description, owner, targets, occupants):
        # define initial variables
        self.name = name
        self.description = description
        self.owner = owner
        self.targets = targets
        self.occupants = occupants
        self.status = ""

    def __str__(self):
        return 'Place('+ str('Name: '+self.name+', Descr: "'+self.description+
                              '", Owner: '+str(self.owner) + ', Targets: '+str(self.targets)+
                              ', Occupants: '+str(self.occupants)+')')

    # setup to show this place details
    def show(self):
        print self.description
        if self.name != "well":
            if self.owner in self.occupants:
                self.status = "open"
                print "The", self.name, "is open!"
                self.showOccupants()
            else:
                self.status = "closed"
                print "We're sorry the", self.name, "is currently closed."
        else:
            self.showOccupants()

        self.showTargets()

    # setup to show available targets of this place
    def showTargets(self):
        if self.targets:
            print "From here you can go: "
            for target in self.targets:
                print "\t", target
    
    # setup to show the occupants of this place
    def showOccupants(self):
        if self.occupants:
            print "Currently here are: "
            for occupant in self.occupants:
                print "\t", occupant

   ### ???? adding, name sais get, **name changed**
    def addOccupants(self, occupant):
        # define how to add an occupant
        self.occupants += (occupant, )
        print 'Add occupants', self.name,self.occupants

    def remOccupants(self, occupant):
        # define how to remove an occupant
        ind=self.occupants.index(occupant)
        if ind > -1:
            if len(self.occupants)==1:
                self.occupants=()
            else:
                self.occupants = self.occupants[:ind]+self.occupants[ind+1:]
        print 'Remove occupants', self.name,self.occupants


places["smith"] = Place(
        "smith",
        "This is the Blacksmith's Shop",
        "grimwo",
        ("well", "bakery", "mill"),
        ()
    )

places["bakery"] = Place(
        "bakery",
        "This is the bakery",
        "berol",
        ("well", "smith", "mill"),
        ("a","b","c")
    )

places["well"] = Place(
        "well",
        "This is the well",
        "berol",
        ("bakery", "smith", "mill"),
        ()
    )

persons["berrol"] = Person("berrol","bakery", "baker") ## person with place and profession

persons["berrol"].setLocation("well")

for p in places: print p, places[p]

This I got when I fooled around with your code a little: ( changed getOccupants to addOccupants to reflect the function)

Thank you for your help TONYJV, I see I was working myself to death there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.