user1276048
user1276048

Reputation: 43

User created classes

I am learning Python, following a book (Python Programming for the Absolute Beginner, 3rd Edition). The book gives an example of a basic Sims like application, where a finite number of "critters" are created, and the user must feed and play with them, increasing their boredom and hunger values. I was practicing using classes, and tried to make my own game. The user was presented with a menu where the user could; buy/create a new sim, delete an old one, or interact with existing ones. The problem I'm having is with creating new classes. The user is initially presented with a screen asking what they would like to do, and if they press X, they are taken to the createnewsim function. However the problem is when a new class is made:

nameofsim = input("what would you like to call your new sim?")
newsim = sim(nameofsim)

If the user were to make another, it would overwrite the existing one, since the name newsim cannot be changed, as I discovered in a previous question, more or less.

So, how do I allocate new object names, or prevent old sims from being overwritten, allowing for the creations of more than one new sim??

Upvotes: 1

Views: 192

Answers (4)

Don Question
Don Question

Reputation: 11624

You didn't provide enough code, to help you sufficiently. So we can't tell if it's possible to save the state of a Sim and continue with a freshly created. It's also unclear what a sim is! Is it the whole game or just a "critter" as you called it?

But in general you could do a mapping in a dictionary like:

sims = {}
nameofsim = input("what would you like to call your new sim?")
if nameofsim in sims:
    newsim = sims[nameofsim]
else:
    newsim = sim(nameofsim)
    sims[nameofsim] = newsim

It also seems you didn't grasp some concepts here:

  1. classes in general are distinct different to objects (in python classes are special types of objects). Objects are to classes in a type-of relationship. This distinction is important because of inheritance and polymorphism in object-oriented paradigms! champ and boy are both dogs, but champ is a labrador and 2 years old and boy is a dobbermann and 5 years old. so both are of the type dog - which makes boy and champs to objects of the class dog!
    As you see, the abstract class dog isn't living - it's just an abstract concept of a being. But the objects champ and boy are the living instances of the class dog!

  2. with the statement newsim = sim(nameofsim) your are doing the following:

    1. creating an object of the type sim
    2. initializining it with the argument provided by nameofsim
    3. assigning this object to an arbitray name -> variable newsim

but the variable newsim is NOT the object! the object may exist even without a variable-name of it's own. The use of a variable just provides you a convenient access to the object.

Upvotes: 0

John La Rooy
John La Rooy

Reputation: 304433

You can make a dictionary of sims like this

sim_dict = {}
while True:
    nameofsim = input("what would you like to call your new sim?")
    if nameofsim == "":
        break
    sim_dict[nameofsim] = Sim(nameofsim)

If the user creates a sim called 'dave' you could access it by using

sim_dict['dave']

But when you are writing the program you can't guess what names the user will give to the sims, so often you will be accessing them in a loop like this

for nameofsim, sim in sim_dict.items():
    if sim.birthday==today:
        sim.age+= 1

Upvotes: 0

Joel Cornett
Joel Cornett

Reputation: 24788

Jakob Bowyer already answered your question, but I thought I'd add a note about Python variables. Consider the following code:

newSim = Sim('Bob')

oldSim = newSim

newSim = Sim('George')

In this example, the end result would be oldSim = Bob and newSim = George. Notice how the object that the variable refers to is not destroyed or overwritten with variable assignment. The only thing that changes is the location in memory that the variable name is referring to.

Upvotes: 0

Jakob Bowyer
Jakob Bowyer

Reputation: 34718

Either store your sim classes in a list or a dictionary; e.g.

class Sim(object):
    def __init__(self, name):
        self.name = name

names = ['tim','dave','jane']
sims = {}
for name in names:
    sims[name] = Sim(name)

Upvotes: 2

Related Questions