Jump to content

Writing Automatic Instance of Class HELP!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
CarpeVexillum

CarpeVexillum

    Newbie

  • Members
  • Pip
  • 2 posts
I am not very experienced in coding as of yet, I have just begun my education in the subject. So I need some advice. I'm writing a program that is a sort of simulation of life (a very simple one). There are people (instances of class Person) which have attributes like name, age, gender (there will be more but I am starting small). So the idea is I, as the user, can run this program with a select number of years. Time is all I control. During this period events will happen: births, deaths, sickness, etc. The program will write out all the events in a txt file so you can see what happened in these peoples lives.

Thats the basic gist of it. My biggest issue right now, is I want automatic births and creation of people. But I don't know how to write a program that automatically creates variables. Usually you have be to like

p0001=Person('John','male',0)

^I can't figure out how to get the variables to make themselves and go so that each time a new person is created it would just go up like p0002, p0003 etc.

Any help would be appreciated, Thank you guys!:)

-Clint

#2
Excited

Excited

    Newbie

  • Members
  • PipPip
  • 27 posts
you can't create variables like that, use a list instead


#example Person class as you didn't supply it
class Person(object):
    def __init__(self, name, gender, x):
        self.name = name
        self.gender = gender
        self.x = x
    def example_method(self, message):
        print self.name + ': ' + message

#list of persons
persons = []

for i in range(10): #10 is the amount of persons
    persons.append(Person('John', 'male', 0))

#access like this
print persons[0].name
print persons[1].gender
print persons[8].example('Hello, how are you?')


#3
CarpeVexillum

CarpeVexillum

    Newbie

  • Members
  • Pip
  • 2 posts
Thank you for the timely response! I don't think I fully understand though, or maybe I didn't explain well enough.

Basically, at some point in the program, it will need to create one new Person. Its not something I can set up in the beginning because it has to happen only if certain conditions are met and once a person is created they have to be unique and have a unique variable name. I think to store all the info I would write it out to a txt file (one txt file per person).

I don't think the list solves this problem, but if it does, woops:-P

#4
Excited

Excited

    Newbie

  • Members
  • PipPip
  • 27 posts
You can add a new instance of Person to the list persons at any point, using the.

read more about lists here: 5. Data Structures — Python v2.7 documentation

#5
spyder

spyder

    Programmer

  • Members
  • PipPipPipPip
  • 120 posts
You could use another array with a tuple of the name, gender, and age.
#example Person class as you didn't supply it
class Person(object):
    def __init__(self, name, gender, x):
        self.name = name
        self.gender = gender
        self.x = x
    def example_method(self, message):
        print self.name + ': ' + message

people_id = [('John', 'male', 200),
			('Susan', 'female', 1)]
			#etc

people = []
n = 0

for name, gender, age in people_id:
	people[n] = Person(name, gender, age)
	n += 1

#rest of program

or if you really wanted to create multiple variables you could use exec.
#example Person class as you didn't supply it
class Person(object):
    def __init__(self, name, gender, x):
        self.name = name
        self.gender = gender
        self.x = x
    def example_method(self, message):
        print self.name + ': ' + message

people_id = [("'John'", "'male'", 200),
			("'Susan'", "'female'", 1)]
			#etc

n = 0

for name, gender, age in people_id:
	exec("p%d = Person(%s, %s, %d) %(n, name, gender, age))
	n += 1

#rest of program
The extra quotes in the names are necessary.
Hope this helps.
I C!(and Python, and C++, and ...)

#6
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
spyder, you can't assign a list index that doesn't exist, you need to use the list.append method ;)

OP you might also want to use dictionaries, which are associative arrays.

#7
spyder

spyder

    Programmer

  • Members
  • PipPipPipPip
  • 120 posts
Good point.
I forgot about that, I just did a quick post.^^
I C!(and Python, and C++, and ...)