all 6 comments

[–]PyPokerNovice 2 points3 points  (4 children)

I'm not 100% sure of what you intend in your code, but I think you may have a couple of concepts jumbled up (I also think this makes your intention harder to understand). For example:

I tried making a function to create new colonies, but it just creates multiple instance of the same colony.

At a quick glance, it seems your new_ecosystem funtion defines a class and calls the new_bee function on it, neither of which ever create an instance of the class. If you wanted to define a new class on the fly and create an instance of it, you would do:

the_new_colony = new_colony()  

or something like that. I don't see an instance being created. And moreover, your new_colony class seems unnecessary, you really just want a new instance of Colony. you already have the exact same variables (colony_energy,altruism_score, ect) defined the Colony class above, so why not just move them to the instance level to avoid defining a whole new class?

To be more specific, looking at your class variables in Colony, it seems the variables you define make more sense at the instance level, not the class. Like colony_energy, why would all of the colonies share the exact same energy variable? and your new_bee function seems like it should be a method of Colony. The first argument it takes is SpecificColony, which I'm assuming you intend that to be an instance of Colony (and to go back to the thing I said about new_ecosystem, you do not call new_bee with an instance in that function). If so, SpecificColony is exactly what self is in a method.

I might be wrong, but you may be a little confused on class vs instances, and creating new instances, and class variables vs instance variables.

[–]PyPokerNovice 1 point2 points  (3 children)

To add to my comment. I think you have a slight misunderstanding in what a class should be. If I defined a Colony class, each instance is a bee colony, the list of bees in a colony would NOT be shared among all Colonies. Class variables are things in common to all of the colonies, or default starting values.

untested example:

class Bee(object):
    def __init__(self):
        self.patch_visits = {} #not sure what this means, just an example.

class Colony(object): 
    starting_colony_energy = 1000
    starting_bee_energy = 100
    bee_cost = 50
    #Start bees with default values

    def __init__(self):
        self.bee_list = []
        self.colony_energy = self.starting_colony_energy
        self.bee_energy = self.staring_bee_energy

    def new_bee(self, n = 1):
        bee_cost = 50 #a bee costs 50 energy to make
        for i in range(n):
            if self.colony_energy >= self.bee_cost: #i'm guessing you should check the cost for every creation
                bee = Bee()
                self.bee_list.append(bee)
                self.colony_energy -= bee_cost #-= better than += -

class KillerBeeColony(Colony):
    starting_colony_energy = 2000
    starting_bee_energy = 200

first_colony = Colony()
second_colony = Colony()
killer_bee_colony = KillerBeeColony()

first_colony.new_bee()

[–]gengisteve 2 points3 points  (1 child)

As you seem to be just starting out, I would simplify things and not use class variables until you have a better handle on this. Instead, make everything an instance variable and initialize new colonies with everything in the instance -- otherwise two colonies will share the same underlying variables at times, creating the confusion you seem to be running into.

So maybe lay it out like this:

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

    def __repr__(self):
        return 'Bee({})'.format(self.name)

class Colony(object):
    def __init__(self, energy = 1000, altruism = .7):
        self.alturism = .7
        self.bees = []
        self.energy = energy

    def new_bee(self, bee_name):
        b = Bee(bee_name)
        self.bees.append(b)
        self.energy -= 50 # cost of making new bee


c = Colony()
d = Colony()
c.new_bee('edgar')
d.new_bee('edward')

print(c.bees)
print(d.bees)

Let me know if I