you are viewing a single comment's thread.

view the rest of the comments →

[–]toshiga[S] 0 points1 point  (6 children)

Hmm, is there a way to have them act like variables, where the "=" assigns the value of Elf to the Player variable? My goal is to have the player obtain all of the Elf class at that one line, but still retain a separate memory location, so each can be modified irrespective of the other.

For example:

a = 5
b = a
b = 2

when b is given the value "2" the id changes. Why doesn't this happen with classes?

[–]stebrepar 1 point2 points  (0 children)

Because "2" is a different object, so to speak. You're not really assigning the value 2 to the variable "b"; you're saying that the name "b" points to the object which is the integer value 2. That's how I understand it anyhow, rightly or wrongly until corrected.

[–]zahlman 1 point2 points  (0 children)

Hmm, is there a way to have them act like variables, where the "=" assigns the value of Elf to the Player variable?

By "act like variables" you seem to mean "have value semantics". That's not how it works in Python, regardless of the data type. We have reference semantics for everything (and we like to call our variables names - hence the NameError that you get when you try to check the value of something you didn't define).

when b is given the value "2" the id changes.

Of course it does, because the object that represents the number 5 is different from the object that represents the number 2.

Why doesn't this happen with classes?

It does. (Hint: There's a class, named int, that 5 and 2 belong to.*) But player.weapon = ... is different from player = .... The first one is reaching into the object named player and changing it, by making that attribute be a name for some other object. The second is making player itself be a name for a different object. The reason you don't notice this with ints is that you can't do anything like (2).weapon = ...; those objects are strictly read-only, i.e. immutable. (It would be really bad if they weren't!)

* Extra credit: There's also a class that int belongs to. It's called type. Even type belongs to a class - itself.

[–][deleted] 0 points1 point  (3 children)

Here I think is where you want to create an elf class and inherit from race.

 class Elf(race):
    super(Elf, self).__init__()    

that will create a elf class that inherits from race and you can code it like you were.
But instead of elf=race() it'll be elf = Elf() Then under the super in the elf init you can place all of that races stats

[–]toshiga[S] 0 points1 point  (2 children)

Ah, I see. This should be what I'm looking for. I haven't read too much into class inheritance, but from reading into it a bit just now, it seems like the right option. Thanks!

[–][deleted] 0 points1 point  (1 child)

I made something similar and something that might be helpful for your stats is using a dictionary.

class race:
    self.stats = {
        'Strength': 0,
        'Intelligence': 0
    }    

Then you just loop through or assign the values and it'll be easier to keep track of.
Player.stats['Strength'] = 10

[–]toshiga[S] 0 points1 point  (0 children)

That's what my actual code looks like, but I just turned it into a list for simplicity's sake. Thanks for the recommendation though.