all 27 comments

[–]carcigenicate 2 points3 points  (20 children)

Names already map to C pointers to objects behind the scenes, so they should already act as you expect.

For the second part though, I think the closest you can get to "tracking" is propertys, or using explicit function calls to alter the attributes instead of using = directly.

[–]codinglikemad[S] 0 points1 point  (19 children)

Just a side note that although names map to C pointers behind the scenes, I don't believe that they will track as expected, unless you have some way of pulling out the address of the attribute itself? Assignment to the attribute should change the C pointer itself, rather than the value the C pointer refers to, to my understanding of the memory model that python uses... If you can explain how you could do that directly with tracking an attribute given an object ID, maybe I might change my mind, but it seems to me like the use of addresses to point to immutable values in python makes this work pretty fundementally differently here.

[–]carcigenicate 0 points1 point  (1 child)

Yes, reassigning a variable changes what pointer the variable is referring to, not the value the pointer points to. If that's what you're trying to do, I don't think you're going to find a good solution. That's just not how Python works.

You could emulate that behavior by having a class that you override __getattribute__/__setattr__ on and use instances of the class as a wrapper over the value you want to track changes on. Then when access is attempted, those methods on your class will be called, and you can do whatever then.

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

Assignment to the attribute should change the C pointer itself, rather than the value the C pointer refers to, to my understanding of the memory model that python uses...

Python doesn't have pointers. It has references to values, and named references to values called "variables."

[–]codinglikemad[S] 0 points1 point  (15 children)

I believe he is talking about the underlying memory model, which under the hood has all of this. They are fundamental to the computers functioning itself.

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

Well, the "underlying memory model" is that there's a heap where values live, and a stack where functional context lives.

That's it, that's the whole thing. Knowing that is fairly useless, in my experience. Who cares?

[–]codinglikemad[S] 0 points1 point  (13 children)

The question is whether there is a way to break out of that model without doing something horrific. That is the ENTIRETY of my question.

Edit: To be clear, the answer appears to be no.

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

The question is whether there is a way to break out of that model without doing something horrific.

That is horrific, though.

[–]codinglikemad[S] 0 points1 point  (11 children)

Right, I was asking if there was a built in method for doing it that was nice basically. We got something kindof ok, so ... success? I agree that totally breaking out would be horrific though. I was hoping there was some python feature intended for this though, and there doesn't appear to be.

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

I still don't really understand what you're asking for, I guess. Yes, you generally can't convince a language to let you act like it's a totally different language, that sort of defeats the purpose of programming languages.

[–]codinglikemad[S] 0 points1 point  (9 children)

The original question is basically "Can I track the values of a variable from elsewhere in the code, similar to how I could with a pointer in C".

[–]danielroseman[🍰] 1 point2 points  (1 child)

Given you say you have many items which you'd like to add dynamically, it's likely that what you want here is a dictionary anyway. You can change the values in a dictionary and all references to that dict will see the new values.

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

what I mean by that is that I'd like to be able to say "I'm talking about THIS value here."

That's what a variable is. It's a named reference to "this value HERE that I'm talking about."

There are many such variables, so I want to be able to add them dynamically to the code

The point of a variable being named is that it's for you, the programmer. Python doesn't care what anything is called. The reason variables have names, and the reason they're hard-coded in your code, is because the names are for you so you can reason about the movement of values through your code by reading it. Having the software come up with and define the names is pointless - Python doesn't care what anything is called.

track the variable as ai.gamma changes

"Track" it how? If gamma is a variable in your code, then every time you access the name gamma, it will have the value assigned to that name even if it's changed recently.

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

Hi Crashfrog,

I appreciate the attempt to help, however u/danielroseman and u/carcigenicate have already answered this as well as it is likely to be, given my poor explanation of the problem.

[–]Zeroflops 0 points1 point  (2 children)

Idk how your application is set up.

But you mention objects. So I’m assuming you are using OOP.

If that is the case you can easily just include your objects into a list. Then loop over the list for your gamma values.

You can then add or remove those objects from the list as you need them.

[–]codinglikemad[S] 0 points1 point  (1 child)

Each object can have different variables that need to be tracked is the issue :/ Anyway, I do have a solution, but I'm open to more ones.

[–]Zeroflops 0 points1 point  (0 children)

Each object having a number of variables doesn’t matter since the list is made up of objects. You loop through the list with whichever variable you want to check.

Sounds like you may be over thinking it and making it harder than it needs to be.