Python Forum

Full Version: how to return a reference to an object?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have a collection of object references in a dict inside a function. i want to have a function that can use give arguments, find the object, and return a reference to it. i am drawing a blank trying to think this up. i want to have the caller increment the number if it's type is int or float and do nothing for anything else. nothing is coded, yet.
What do you mean by object references? A name that the object has been assigned to or something else? Can you show how your dict is constructed?
no code, yet. nothing to show. i have not decided how to construct the dict. i could use a list or two if i need to. suggestions?
(Jun-06-2023, 02:16 AM)Skaperen Wrote: [ -> ]suggestions?
Just return the object. Python always returns objects by reference.
(Jun-06-2023, 08:44 AM)Gribouillis Wrote: [ -> ]
(Jun-06-2023, 02:16 AM)Skaperen Wrote: [ -> ]suggestions?
Just return the object. Python always returns objects by reference.

i want to increment the object. i can do this when i get it directly from a dict like so:
cars_per_train[which_train] += 1 # one car passes by so count it.
if i make a different function to do the increment, i can make work. but i want to have it all done with one function that simply returns the object by its key (which_train).
(Jun-07-2023, 01:06 AM)Skaperen Wrote: [ -> ]i want to increment the object. i can do this when i get it directly from a dict like so:
cars_per_train[which_train] += 1 # one car passes by so count it.

If the value in that dict is an immutable object (like an int), then you can't really increment that object. Instead, a different object will be assigned to that key in the dict. As such, it's unclear if you really want just the int object, as it cannot be changed.

If the value is mutable, then incrementing is possible. In that case, just return the value.
If the object is immutable, it cannot be "passed by reference". No changes can be made to the object, so any operation, like incrementing, creates a new object. If the object is mutable, it is always "passed by reference". Changes made to the object will be seen by anyone who references the object.

Sounds like you need to create a mutable object that behaves like a number, at least some of the time, but can have it's "value" changed.
(Jun-07-2023, 01:06 AM)Skaperen Wrote: [ -> ]f i make a different function to do the increment, i can make work. but i want to have it all done with one function that simply returns the object by its key (which_train).
Please describe the whole problem with the cars and the trains and what each function should do (detailed arguments, effect, return value). You make us guess both the problem and the solution. Frankly I still don't understand what you want to do and why.
(Jun-07-2023, 04:37 AM)Gribouillis Wrote: [ -> ]
(Jun-07-2023, 01:06 AM)Skaperen Wrote: [ -> ]f i make a different function to do the increment, i can make work. but i want to have it all done with one function that simply returns the object by its key (which_train).
Please describe the whole problem with the cars and the trains and what each function should do (detailed arguments, effect, return value). You make us guess both the problem and the solution. Frankly I still don't understand what you want to do and why.

i want to avoid writing code that accesses the dict directly and, instead, wrap the dict with an API.