tags:

views:

182

answers:

7

I'm still new to Python and Programming in general, so I need simple explanations! I don't even know what this dictionary thing you'r talking about is!

I'm trying to create a game for my little sister. It is a Virtual Pet sort of thing and the Pet has toys to play with.

I created a class Toy and want to create a function, getNewToy(name, data1, data2, data3, data4, data5).

I want this function to create a new instance of the class Toy, and I want the function to be able to be called multiple times each time creating a new instance.

In my experience you create an instance with:

class Toy:
    def __init__(self, name, data1, data2, data3, data4, data5):
        pass

myToy = Toy(myToy, 1, 2, 3, 4, 5)

then to use methods from the class with:

myToy.method1()

Seeing as I want to have the ability to have multiple toys, each with a playWith() method I want the instance to reflect the name of the Toy each time one is called.

I want the instance to be different each time I call the method getNewToy(,...) and the instance to reflect the name.

Remember I'm new to programming, so can you keep explanations simple.

A: 

Let's say you have three classes: Enemy1, Enemy2, Enemy3. This is how you instantiate them directly:

Enemy1()
Enemy2()
Enemy3()

but this will also work:

x = Enemy1
x()
x = Enemy2
x()
x = Enemy3
x()

Is this what you meant?

Claudiu
+2  A: 

If you haven't found it yet, here is Dive into Python's chapter on object-oriented programming.

Here are some more examples, scroll to BankAccount.


You can call a class directly to create an instance. Parameters are passed to the __init__ method.

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

imouto = Tamago('imouto')
oba = Tamago('oba')
oba.name # 'oba'
imouto.name # 'imouto'
Tobu
But I would like the function to be called multiple times, each creating a new instance of the clas...
Jasper
You can, please try.
Tobu
A: 

This is a very strange question to ask, specifically of python, so being more specific will definitely help me answer it. As is, I'll try to take a stab at it.

I'm going to assume what you want to do is create a new instance of a datastructure and give it a variable. For this example I'll use the dictionary data structure and the variable mydictionary.

mydictionary = dict()

This will create a new instance of the dictionary data structure and place it in the variable named mydictionary. Alternatively the dictionary constructor can also take arguments:

mydictionary = dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])

Finally, python will attempt to figure out what data structure I mean from the data I give it. IE

mydictionary = {'jack': 4098, 'sape': 4139}

These examples were taken from Here

tzenes
A: 

I think you can use eval. Something like this

def toclass(strcls):
    return eval(strcls)()
The usage of eval should be avoided. Also this method requires the class to be accessible under the name of the class. Suppose you want to be able to instantiate a class named Foo which is in the module bar, your function requires the user to know the exact location. What if you put the class Foo in the same module as your function at a later point or the other way around? Also your function doesn't return a class although the name suggests so.
DasIch
Ya I know. There is a messup between the name and return value. Just want to give him another way to go.
+2  A: 

Given your edit i assume you have the class name as a string and want to instantiate the class? Just use a dictionary as a dispatcher.

class Foo(object):
    pass

class Bar(object):
    pass

dispatch_dict = {"Foo": Foo, "Bar": Bar}
dispatch_dict["Foo"]() # returns an instance of Foo
DasIch
A: 

If you just want to pass a class to a function, so that this function can create new instances of that class, just treat the class like any other value you would give as a parameter:

def printinstance(someclass):
  print someclass()

Result:

>>> printinstance(list)
[]
>>> printinstance(dict)
{}
sth
A: 

Rather than use multiple classes or class inheritance, perhaps a single Toy class that knows what "kind" it is:

class Toy:
    num = 0
    def __init__(self, name, kind, *args):
        self.name = name
        self.kind = kind
        self.data = args
        self.num = Toy.num
        Toy.num += 1

    def __repr__(self):
        return ' '.join([self.name,self.kind,str(self.num)])

    def playWith(self):
        print self

def getNewToy(name, kind):
    return Toy(name, kind)

t1 = Toy('Suzie', 'doll')
t2 = getNewToy('Jack', 'robot')
print t1
t2.playWith()

Running it:

$ python toy.py 
Suzie doll 0
Jack robot 1

As you can see, getNewToy is really unnecessary. Now you can modify playWith to check the value of self.kind and change behavior, you can redefine playWith to designate a playmate:

def playWith(self, who=None):
    if who:  pass
    print self

t1.playWith(t2)
telliott99