2

OK. I'm setting up a model which will describe the effects of a number of management decisions on forests. Each forest is an instance of the forest class and you can see a simplified version below:

class forest():
     instancelist = [] # a list of all the forest instances so I can run functions on all of them at once

     growth_rate = 2 #very simple rate of growth (not realistic!)
     felling_year = 50 #all forest areas are felled at age 50

     def __init__(self, x=0, y=0,age=0,size=0): 
         self.instancelist.append(self) # add the forest area to the instance list
         self.x = x # x coordinate
         self.y = y # y coordinate
         self.age = age # age, not all forests are planted on bare sites, - we have some pre-existing ones to consider.
         self.size = size # very rough - but this is an indicator of the physical volume of timber (not area)

I can now generate a forest object, for example:

f = forest(1,1,20,40)

So, the difficulty I'm having is that I need to generate a number of forest blocks (so we see what the effect is on a wider area). To do this, I'll need to create a lot of areas.

I can do this easily if I don't specify any properties:

forests = [forest() for x in range(20)]

But I can't see how to generate a large number of areas each with unique properties without going through and specifying them all by hand. Is there a method that I can use where I feed in data from another source (lists, tuples, csv, whatever) and use it to build up an inventory of different objects?

Sorry if this is a stupid question (I have been known to ask them from time to time) but it's really confusing me.

3
  • Where are the properties stored? It seems there are 4 of them. Do you therefore have a list of lists [with each inner list having 4 items]? Or a generator? Commented Apr 13, 2018 at 14:26
  • There are 4 in this example -it's all stripped down, but there will probably be a lot of others. I don't have them all added yet, so I could produce them in whatever format works best really. Eventually, I'll probably need to use some form of configuration file for the starting conditions - but that's a bit of a way off yet. Commented Apr 13, 2018 at 14:30
  • OK, I guess your problem should be reduced to reading the config file into a list of lists, dictionary or other format. Then use a list comprehension. Commented Apr 13, 2018 at 14:32

1 Answer 1

2

Here is one way if you store your properties in a list of lists:

class Forest(object):
    def __init__(self, w, x, y, z):
        self.w = w
        self.x = x
        self.y = y
        self.z = z
        return None

properties = [[1, 2, 3, 4],
              [5, 6, 7, 8],
              [9, 10, 11, 12],
              [13, 14, 15, 16],
              [17, 18, 19, 20]]

forests = [Forest(*p) for p in properties]

print(forests[1].x)  # 6
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, this looks like what I'm after, quick couple of questions: in forests = [Forest(*p) for p in properties] what is the *p doing? also, why is the object needed in class Forest(object):
@Will *p is called argument unpacking. It takes some iterable and unpacks it to populate the arguments. So Forest(*[1, 2, 3, 4]) becomes Forest(1, 2, 3, 4) after unpacking. The object is the old fashioned/Python 2 way of declaring classes (because every class is a child class of the object class). In Python 3, you can omit that and just do class Forest:, but including it doesn't hurt anything.
@PatrickHaugh Actually, in Python 2, every class is not a subclass of object unless you specifically ask for it like this (and you don’t get some modern features without it); in Python 3, every class is a subclass of object (with all the extra features), so you don’t need to specify it anymore.
@Will The nice thing about this answer is that the same code will work if properties is a list of dicts with keys w, x, y, and z just by changing the * to a **, and a dict like that stored in a JSON or YAML or CSV config file is a great way to make them human-readable and -editable, so this gives you room to grow into whatever design you want.

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.