tags:

views:

170

answers:

4

What's the best name to describe this:

class Animal(object):
    def __init__(self,animal_type):
        self.animal = animal_type()

class Dog(object):
    def __init__(self):
        pass

fido = Animal(Dog)

?

In particular, this line:

self.animal = animal_type()

Basically what is happening is I'm passing the type of the class I want to initialize as a parameter.

TIA, Noah

A: 

Depending on how you use it, it might be the Decorator pattern

EDIT: Just to make it clear, the decorator pattern is not Python specific, it's an object-oriented design pattern applicable to any object-oriented language.

Regarding the line self.animal = animal_type(), what you are actually doing is calling a Python type, which returns a new instance.

For example, you could instance an integer by calling the integer type:

>>> a = int()
>>> a
0
>>> type(a)
<type 'int'>
>>> a = int(3)
>>> a
3
>>>

As you can see, you can also pass parameters to a type when calling it, which will be used for initialization. In your case, you are instancing a Dog object by passing the class type (which is just another Python object). I'm not sure if there's a specific name for this.

sttwister
+3  A: 

It would probably be suitable to say that types are "first class objects" in Python, meaning that they can be passed around just like any other object.

Mike Graham
A: 

I don't know if there is an "official" name for it, but I think a good name would be type injection.

Dave Kirby
Dave, you get the prize for most creative name. I'm groping for the joke here, all I know is that it involves a turkey baster.
Noah
Hot type injection? *shudder*
jathanism
I'm going to call all parameter passing "injection" from now on! `x = sqrt(2) # integer injection`
Ken
It's also know as the Factory pattern, especially when you generalize it to passing in any callable that returns a new instance.
Shane Holloway
It's not called "type injection". If you say that to someone else they're going to have no idea what you're talking about. If someone's asking if something has a name, don't just make something up.
Glenn Maynard
@glenn: all names were made up by someone once.
Dave Kirby
@Dave Kirby: I'm going to call you "Jane" from now on. You already have a name? Well, I think "Jane" is a better name, so I'll go with that. That's not your name and nobody will know who I'm talking to? Hey, all names were made up by someone once, and I just made up "Jane", so it's just as valid as "Dave Kirby".
Glenn Maynard
@Ken -- http://ruby-doc.org/core/classes/Enumerable.html#M003129
Noah
@Glenn, they won't know what you're talking about until you explain it. Then they will make a joke about a turkey baster.
Noah
A: 

It's called a class factory: an object that, when called as a function, instantiates and returns an object. All Python classes are also class factories. It's not often referred to as that in Python, since it's such an implicit concept in this language, but if you're looking for a name, that's what you're looking for.

Glenn Maynard
It's not a "type" factory, the type is the object being called (the class in this case). A better name would be instance factory.
sttwister
I've edited it to "class factory", which is more generally used. (My habit is "type factory", which is what I learned it as, but Google points "class factory" being more widely used.) It's a factory that instantiates types or classes. Maybe "instance factory" would be better, but that's just not what it's called.
Glenn Maynard
I would have thought a class factory would create a new class, rather than instantiate an instance of an existing class.
Dave Kirby