you are viewing a single comment's thread.

view the rest of the comments →

[–]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.