Author Topic: Oh boy, adventures in rebinding variables!!! (Python)  (Read 32236 times)

chooseusername

  • Rogueliker
  • ***
  • Posts: 329
  • Karma: +0/-0
    • View Profile
    • Email
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #15 on: May 20, 2014, 05:02:54 AM »
Ah, so it's a mutable/immutable thing.  Okay.  That makes sense, because tuples seem to behave the same way, based on my limited testing.

Lets walk through this just to makes sure I understand:

The first variable x gets bound to five.  This means 5 is used to generate a hash value (which is also 5, it seems based on testing), which python uses to decide what memory address to store the value '5' at.

Second variable x gets bound to five.  Now, I though that with a pointer, a new hash value was generated for a new memory address, and that address just stored the location of the first memory address and told the computer to look there next.

But is the following happening instead for the second x?: python generates an identical hash for the second x, linking both x's back to the same memory address containing the 5 value.

See the distinction I'm trying to understand?  With what I understand a pointer to be (and I may be totally wrong) is one address that just contains instructions which 'point' the program to another different address, but with what seems like it is happening in python, there is only one address that both variables refer to.

And you guys are saying that the process is different with mutables/immutables?  Which, if either, of the scenarios I described previously happens with each of those?

I know theses seem like odd questions, but I think about things in an odd way, and this really does help me.
It is not a mutable/immutable thing, you can find reasoning in my previous answers that make it clear that this cannot possibly be the case were you to read and understand it.  I have no idea what you are talking about with respect to hashes and memory addresses - this is complicated nonsense you've constructed somehow.  Many programming languages do not feature pointers, but are implemented using pointers.

If you really wanted to understand how the variable scoping worked, you would google it and find lots of explanations and answers.  And you'd experiment in the interpreter and reconcile the facts.  And like other experienced programmers, you'd pass this bit in several minutes.  But instead you post nonsensical and often pointless questions, ignore some facts provided in various answers and leap on offhand mentions provided in various answers.

It's one thing to have a different learning style, and another to seek procrastination by whatever it is you are doing.

Gr3yling

  • Rogueliker
  • ***
  • Posts: 168
  • Karma: +0/-0
    • View Profile
    • Email
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #16 on: May 20, 2014, 05:41:56 AM »
chooseusername,

I really appreciate you taking the time to make your most recent post and all the other ones in this topic.  I realize that you have been much more patient than most people would have been with the questions I have asked.

That said, please do not post in my topic again.  I realize I have no authority to compel you not to do so, but I would greatly appreciate it if you did not.  I am sorry if I offended you with something I have said previously, but I do not feel that you are making a good faith attempt to be helpful anymore.

Xecutor, if you, Trystan, Zireael, and Aukustus all feel that this topic is a waste of time as chooseusername does, I will be happy to close it.  Otherwise, I look forward to your continued input, as I think you are knowledgeable enough to help me with what I am trying to understand.

*Edited because I can't spell.
« Last Edit: May 20, 2014, 05:43:48 AM by Gr3yling »

Trystan

  • Rogueliker
  • ***
  • Posts: 164
  • Karma: +0/-0
    • View Profile
    • my blog
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #17 on: May 20, 2014, 09:33:57 AM »
It's not a waste of time if it helps you.

But is it helping you? It's very rarely useful to know the low level details of a language like this especially since id is a function you almost certainly won't use.

Aukustus

  • Rogueliker
  • ***
  • Posts: 440
  • Karma: +0/-0
    • View Profile
    • The Temple of Torment
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #18 on: May 20, 2014, 10:04:34 AM »
I have nothing against this thread. Feel free to continue posting your problems and we shall help you if we can.

Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #19 on: May 20, 2014, 03:28:46 PM »
Quote from: Gr3yling
Second variable x gets bound to five.  Now, I though that with a pointer, a new hash value was generated for a new memory address, and that address just stored the location of the first memory address and told the computer to look there next.
<...>
With what I understand a pointer to be (and I may be totally wrong) is one address that just contains instructions which 'point' the program to another different address, but with what seems like it is happening in python, there is only one address that both variables refer to.
The numbers you observe (hashes and hypothetical memory address) are actually the result of an optimization and not the optimization mechanism itself. When you ask for the second variable's properties, interpreter silently follows all those internal pointers and gives you the final result. Therefore for external observer it looks like two variables are the one and the same.

Also, do not confuse Python variables and real memory blocks. In lower-level languages like C they are the same thing. In higher-level languages variable is more like a wrapper around some object and may have very complex relation to actual memory. There is no reason for some variable to point to another variable's data indirectly. Because variable and it's data are not the same here, both variables may just directly point to same data.
« Last Edit: May 20, 2014, 03:37:47 PM by Cfyz »

Gr3yling

  • Rogueliker
  • ***
  • Posts: 168
  • Karma: +0/-0
    • View Profile
    • Email
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #20 on: May 20, 2014, 06:39:29 PM »
Also, do not confuse Python variables and real memory blocks. In lower-level languages like C they are the same thing. In higher-level languages variable is more like a wrapper around some object and may have very complex relation to actual memory. There is no reason for some variable to point to another variable's data indirectly. Because variable and it's data are not the same here, both variables may just directly point to same data.

Darn.  I guess I am in over my head, then.  Trystan, you're right, I thought this was going to help my understand why I keep having problems with my variables, but I guess it won't after all.

Like I mentioned earlier, my python book should be here pretty soon.  I guess I just need to study more to understand where I'm going wrong.


mushroom patch

  • Rogueliker
  • ***
  • Posts: 554
  • Karma: +0/-0
    • View Profile
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #21 on: May 21, 2014, 12:11:11 AM »
To answer your question: When python sees (i.e. parses) a numeric, string, or other immutable literal (e.g. 5), it either allocates memory for the corresponding object or recognizes it as another copy of a literal already encountered, in which case it uses the object already allocated. Variables assigned to the same numeric literal (5) therefore have the same "id."

I would also agree with chooseusername. You should ask questions like this on stackexchange or similar sites, if you must (where you will encounter appropriate criticism for them, I might add), or just google. Questions about the mechanics of id or python hashes or whatever else are both uninteresting and better addressed by people well versed in and interested in answering questions about such matters, rather than people who don't know but are eager to indulge your idle curiosity.

Gr3yling

  • Rogueliker
  • ***
  • Posts: 168
  • Karma: +0/-0
    • View Profile
    • Email
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #22 on: May 21, 2014, 01:06:06 AM »
I would also agree with chooseusername. You should ask questions like this on stackexchange or similar sites, if you must (where you will encounter appropriate criticism for them, I might add), or just google. Questions about the mechanics of id or python hashes or whatever else are both uninteresting and better addressed by people well versed in and interested in answering questions about such matters, rather than people who don't know but are eager to indulge your idle curiosity.

As best as I can determine from my examination of the programming forum, there have been no posts in topics other than this one since May the first.  No precious resources that should have been allocated to other concerns have been squandered.  No one is wasting your valuable time and alleged expertise. This post is not off topic.  It is about the  difficulties I have had programming a roguelike and it was posted in the programming forum.  If you feel that it violates any kind of rules, by all means, report it to the moderators.

I find it somewhat surprising that you chose to belittle me by saying that this topic satisfied my "idle" curiosity.  One wonders, do you find your own posts about video game development in an extremely niche genre to be somehow more productive to society?  Do you arrogantly find your own contributions to this forum to be somehow vital to its functioning? 

I also find it interesting that you state that others who have made contributions to this topic, like Xecutor, Aukustus, and Zireael "don't know" the answer to my question.  And I suppose you think you know far better than them?  I personally fail to see how you have distinguished yourself as being more knowledgeable to them.

Finally, I posted at least in part because of the desire to interact with other individuals on these forums who I like and respect.  We could have had an enjoyable discussion, if nothing else, had you and chooseusername not come along. 

I wish to do absolutely nothing on this forum other than enjoy exchanging ideas and support other people who are trying to develop a game just as I am doing.  I don't understand why you don't feel the same way.

mushroom patch

  • Rogueliker
  • ***
  • Posts: 554
  • Karma: +0/-0
    • View Profile
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #23 on: May 21, 2014, 02:21:46 AM »
I don't know how else to account for a question about the "id" method that anyone who really cared could answer with a few minutes of googling (or, alternatively, by actually listening to the people who answered the question in this thread) than "idle curiosity." I answered your question marginally more completely than others in the thread, who you have also ignored as far as I can tell, except to the extent that they have offered validation of whatever it is you're trying to accomplish by this question.

As to why I don't feel exactly the same way as you: I hope to learn something, not find emotional support. It strikes me as kind of self-indulgent to start a thread nominally about a point of trivia in python programming primarily for the opportunity to chit chat. Anyway, from my perspective, your cri de coeur here only erodes my confidence that my goals of actually learning something will be achieved often enough to justify reading any given thread. I suspect there are others who might put the point more delicately, but feel roughly the same way.

guest509

  • Guest
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #24 on: May 21, 2014, 02:23:30 AM »
Gr3yling ignore the blather. You can continue to post programming questions here. It's the reason we have  a programming section.

If people do not know the answer you'll have to go somewhere else. No biggie.

A lot of pissy moods around these days. The internet is dark and full of terrors.

Trystan

  • Rogueliker
  • ***
  • Posts: 164
  • Karma: +0/-0
    • View Profile
    • my blog
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #25 on: May 21, 2014, 02:29:17 AM »
understand why I keep having problems with my variables, but I guess it won't after all.

Is there a specific problem you are running into or just unsure about python internals and the id function?

mushroom patch

  • Rogueliker
  • ***
  • Posts: 554
  • Karma: +0/-0
    • View Profile
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #26 on: May 21, 2014, 02:48:35 AM »
Gr3yling ignore the blather. You can continue to post programming questions here. It's the reason we have  a programming section.

If people do not know the answer you'll have to go somewhere else. No biggie.

A lot of pissy moods around these days. The internet is dark and full of terrors.

Pretty lame. His question has been answered at least twice over.

If questions like this are the reason there's a programming section, I'm not surprised it goes weeks without substantive activity.

Gr3yling

  • Rogueliker
  • ***
  • Posts: 168
  • Karma: +0/-0
    • View Profile
    • Email
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #27 on: May 21, 2014, 03:25:13 AM »
Yes.  There is a specific problem.  It's kind of hard for me to know exactly what code to post (I'm so lost It's really hard to be sure).  Also, my code is really ugly.   The code is based on the complete roguelike tutorial code, but I've tried to come up with my own way to do as many things as I can, which is probably why it doesn't work now. Anyway, maybe this is a decent start.

the monster AI module is supposed to be called after the player has moved, and this is my attempt to do that:

Code: [Select]
def monster_act():       
    if player_action != False:   
        if game_state == 'playing':
            for object in objects:
                if object.ai == Hostile_AI:
                    if object.alive:
                        object.Hostile_AI.take_turn()

Problem is, I get an exception saying that my object class (of which monsters are a member), doesn't have an attribute "Hostile AI"

Well, here is my object class template:

Code: [Select]
class Object:
    def __init__(self, x, y, char, name, color, ai = None, item = None, status = None, equipment = None, inventory = None, alive = True, blocks = False):
        self.x = x
        self.y = y
        self.char = char
        self.name = name
        self.color = color
        self.ai = ai
       
        if self.ai:
            self.ai.owner = self
       
        self.item = item
       
        if self.item:
            self.item.owner = self
       
        self.status = status
       
        if self.status:
            self.status.owner = self
       
        self.equipment = equipment
       
        if self.equipment:
            self.equipment.owner = self
       
        self.inventory = inventory
       
        self.alive = alive
        self.blocks = blocks

There's more code for object, but this is already a ton, so I won't post it unless you tell me to.

And here's a specific instance of object:

Code: [Select]
       
monster = Object(x, y, 'R', 'Razor head ' + str(razorhead_no), libtcod.black, ai = Hostile_AI,
status = razorhead_status, alive = True, blocks = True)
               
razorhead_no = razorhead_no + 1

Hostile ai is there!  I don't see what the problem is.

The whole idea behind my question was that I thought if I knew how python handled attributes/variables, that I would be able to figure out myself what went wrong.

*Edited for clarity
« Last Edit: May 21, 2014, 03:27:29 AM by Gr3yling »

tuturto

  • Rogueliker
  • ***
  • Posts: 259
  • Karma: +0/-0
    • View Profile
    • pyherc
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #28 on: May 21, 2014, 03:38:27 AM »
You are trying to use attribute named Hostile_AI, while the correct attribute is ai (I'm assuming that Hostile_AI has method take_turn()).

Key is in Object's __init__: self.ai = ai. You're assigning to Hostile_AI to ai attribute.
Everyone you will ever meet knows something you don't.
 - Bill Nye

mushroom patch

  • Rogueliker
  • ***
  • Posts: 554
  • Karma: +0/-0
    • View Profile
Re: Oh boy, adventures in rebinding variables!!! (Python)
« Reply #29 on: May 21, 2014, 04:00:46 AM »
Wow, you were right. This is turning into an enjoyable discussion. So what does the interpreter say after you fix that line?

(i.e. when you change object.Hostile_AI.take_turn() to object.ai.take_turn() ?)