0

I am trying to make it so that when I can run a function which would populate a class with a couple of instances which are contained within the code.

class Pets(object):

  def __init__(self, name, scientific_name, feet_number, type)
    super(Pets,self).__init__()
    self.name = name
    self.scientific_name = scientific_name
    self.feet_number = feet_number
    self.type = type

This is the point where I get stuck. I want to make a function which has a list of instances (Ex. a dog, a cat, a horse...) so that when the function is run those instances can be accessed immediately.

I know from places like Creating dynamically named variables from user input (Second Paragraph, First Sentence), that what I'm asking for is possible, I just don't know the syntax for it.

Community
  • 1
  • 1
Eero Ward
  • 5
  • 4
  • Why are you calling `super(Pets, self)`? What constructor do you intend this to run? – nbryans Jun 28 '16 at 22:30
  • This is a very confusing question. You can run code like `my_instance = Pets('bob', robert, 2, my_type)` from a function which will create an instance with those values. Do you mean something else? – joel goldstick Jun 28 '16 at 22:33
  • I was just taught to use always use super in classes in case I need to create inheritance later. – Eero Ward Jun 28 '16 at 22:33
  • @joel goldstick, I want to make a function which as a list of instances (Ex. a dog, a cat, a horse...) so that when the function is run those instances can already be accessed – Eero Ward Jun 28 '16 at 22:38
  • So where do you want to instantiate those instances? – joel goldstick Jun 28 '16 at 22:40
  • I was thinking of putting it in a function at the beginning of script that I would just call the first time it was run. – Eero Ward Jun 28 '16 at 22:42
  • You only need `super` if you're already inheriting from another class (`object` is different) *and* you want to run its `__init__()` function, too. – MattDMo Jun 28 '16 at 22:47
  • @MattDMo You also need `super` if you intend for other classes to inherit *from* your class in a cooperative inheritance hierarchy. – chepner Jun 28 '16 at 23:56
  • You don't populate a class with instances of itself. A class defines a type; it is not a container. – chepner Jun 28 '16 at 23:59
  • @chepner ah, I did not know that. TIL :) – MattDMo Jun 29 '16 at 13:01
  • @joelgoldstick, but how would I do that exactly? – Eero Ward Jun 29 '16 at 13:07

1 Answers1

0

Is this what you are trying to do?

class Pet(object):
  def __init__(self, details):
    self.name = details[0]
    self.scientific_name = details[1]
    self.feet_number = details[2]
    self.type = details[3]

if __name__ == '__main__':
    pet_list = [('Cat', 'Kitty Cat', 4, 'Cuddly'), ('Dog', 'Puppy Wuppy', 3, 'Licky')]
    pets = [Pet(item) for item in pet_list]

Which gives you:

pets
> [<__main__.Pet at 0x8134d30>, <__main__.Pet at 0x8134d68>]

pets[0]
> <__main__.Pet at 0x8134d30>

pets[0].name
> 'Cat'

pets[0].scientific_name
> 'Kitty Cat'

pets[1].name
> 'Dog'

There are a lot of ways this could be put together depending on what you want to do. For example, you could make a master class called Pet() with some basic attributes and methods that are true for all pets, then create specific classes for each pet that inherit the base class, e.g. class Cat(Pet):

Or you could give the Pet class the ability to know all the other details depending on what name is passed into it, then populate the instance variables accordingly.

Jeff
  • 2,158
  • 1
  • 16
  • 29
  • Thank you for the answer, this is exactly what I was looking for, I just needed to change the way I wanted to structure my code slightly. Again thank you. – Eero Ward Jun 30 '16 at 20:11