开发者

Creating an Instance of a Class with a variable in Python

开发者 https://www.devze.com 2022-12-17 08:30 出处:网络
I had this question posted earlier, but it wasn\'t very clear, and I had trouble with the answers. Since I edited it to make MUCH more sense it seems that people haven\'t been looking at it, perhaps b

I had this question posted earlier, but it wasn't very clear, and I had trouble with the answers. Since I edited it to make MUCH more sense it seems that people haven't been looking at it, perhaps because they see it already has 6 answers. So I'm re=-posting it here:


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're 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.

T开发者_Python百科hank you very much, it's much easier to understand now!


Heres how I would do what you explained:

# The following two classes are toys, both have a playWith 
# as you wanted, each playWith do different things
class Ball:
    def __init__(self):
        self.name = "ball"

    def playWith(self):
        print "the ball bounces"

class Car:
    def __init__(self):
        self.name = "car"

    def playWith(self):
        print "the car is fast"

# This is a Python generator, every time .next() is called on it,
# the next "yield-value" is returned
def generator():
    while True:
        yield Ball()
        yield Car()

# This is the creator, it has to be a class rather than a function
# since you wanted a new toy each time getNewToy is called
# and as such the generator needs to be tracked
class ToyCreator:
    def __init__(self):
        self.generator = generator()

    def getNewToy(self):
        return self.generator.next()

# Create five toys, print their name and play with them
# Do note here that even though we ask for five toys but only have
# two "yields" in the generator, the generator "wraps around" (since,
# internally, its just an endless loop) 
toyCreator = ToyCreator()
for i in range(5):
    toy = toyCreator.getNewToy()
    print "Toy",i,toy.name,"\t:",
    toy.playWith()

If youre having trouble understanding the yield business, take a look at the documentation for the python generator.

What youre trying to do is implement a design pattern, a factory pattern, to be more precise.

Still feeling confused? Read again, think a little but dont hesitate to ask. Were here to help. :)


There is no point in making a special getNewToy function. Just create the class:

 newtoy = Toy(name, data1, data2, data3, data4, data5)

That's all you need to do.

"method I want the instance to reflect the name of the Toy each time one is called."

class Toy:
    def __repr__(self):
        return "<type Toy name=%s>" % self.name


I don't see why you need a getNewToy method. Every time you call Toy() you will get a new instance of your class. You probably want code like this:

class Toy: 
    def __init__(self, name, data1, data2, data3, data4, data5): 
        self.name = name
        self.data1 = data1
        # ...

myToy = Toy("firsttoy", 1, 2, 3, 4, 5)
myToy2 = Toy("2ndToy", 6, 7, 8, 9, 10)


Perhaps you will always have 5 data items, but even if so, consider using *args:

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

精彩评论

暂无评论...
验证码 换一张
取 消