0

I am working on a project that I need to create some objects dynamically from a class. I am not asking for anyone to solve it but just point me in the right direction. An example would be if I was working with Dogs.

class Dog(object):
    """Creating Dogs"""
    def __init__(self, name, age, gender):
        super(Dog, self).__init__()
        self.name = name
        self.name = age
        self.name = gender

I would like however to have a function that I can pass this information into that would create a global object. The function would look like this:

def create_dog(name, age, gender):
    name = Dog(name,age, gender)
    return(name)

So theoretically if I passed the name "Jack" to the function I would have a object globally called "Jack" from the class. I am just trying to find a good way to do this if any. If this is completely against python I will figure out another way but I am sitting here stuck. I have also thought about nesting the class within my function but the namespace won't go into global.

zondo
  • 19,901
  • 8
  • 44
  • 83
  • Is there a reason for having a bunch of global objects? Generally it's best to avoid global objects when possible because the code can become very difficult to debug. – mxdg Apr 07 '16 at 13:44
  • 1
    Not 100% sure what you're asking, but possible duplicate of [How do I do variable variables in Python?](http://stackoverflow.com/q/1373164/953482). Short version: it's almost never a good idea to have a function `create_dog("Jack", 5, "male")` that creates a variable in the global scope whose name is `Jack`. – Kevin Apr 07 '16 at 13:53

3 Answers3

0

Maybe you want to make an alternative constructor ?

class Dog(object):
    """Creating Dogs"""
    def __init__(self, name, age, gender):
        super(Dog, self).__init__()
        self.name = name
        self.name = age
        self.name = gender


   @classmethod
   def create_dog(name, age, gender):
       name = Dog(name,age, gender)
       return name

See also discussion Meaning of @classmethod and @staticmethod for beginner?

A global dictionary is fine if you use a single file script, but for a more complex software you'll have to import all the 3 elements:

 from dogs import dogs, create_dog, Dog

With class attribute you import just one name space. So you could do this:

class Dog(object):
    """Creating Dogs"""

    members = []

    def __init__(self, name, age, gender):
        super(Dog, self).__init__()
        self.name = name
        self.name = age
        self.name = gender


   @classmethod
   def create_dog(name, age, gender):
       name = Dog(name,age, gender)
       Dog.members.append(name)
       return name

Now you can simply import Dog, and access the class attribute Dog.members

Community
  • 1
  • 1
oz123
  • 27,559
  • 27
  • 125
  • 187
  • Thanks Oz. I am definitely going to read much more on this. I think I am going to lean towards to the dictionary for this instance, but I have ran into this question a couple of times. – kaceymusgraves Apr 07 '16 at 14:10
  • you could modify the constructor to update the class, and the you don't have a global variable rather a class attribute. – oz123 Apr 07 '16 at 14:24
0

you could create a global dictionary

dogs = {}

def create_dog(name, age, gender):
    # maybe check if already exists
    dogs[name] = Dog(name, age, gender)
Pedru
  • 1,430
  • 1
  • 14
  • 32
0

With changing locals: (which is still bad, but not as much as changing globals)

def create_dog(name, age, gender, locals_=locals()):
    dog = Dog(name, age, gender)
    locals_[name] = dog


create_dog("jack", 3, "male")
print(jack)
pacholik
  • 8,607
  • 9
  • 43
  • 55