Python Forum

Full Version: identifying a dictionary with an attribute?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i will have a dictionary of objects. each object will have some key that identifies it in the big dictionary. the objects are dictionaries, too (so it's a dictionary of dictionaries). when processing an object, there a many functions involved to which a reference to the object will be passed. i would like to also include the object's identity within the object itself. the big question i have is what key to store it under, given that each object could potentially have data items that could conflict with they key chosen to story its identity. the defaultdict is close but not the solution. it stores that default somewhere; it just uses it in a way that is incomparible with this problem. so i'm thinking i could store the identity as an attribute. would this be possible?
It's already there, type (classinfo):
type(object)
to use in conditional
if isinstance(object, type):
    ...
https://docs.python.org/3/library/functions.html#isinstance
(Oct-03-2018, 02:51 AM)Skaperen Wrote: [ -> ]the object's identity

id(object)
so how do i set the object's identity? suppose i have an object of type dictionary (any possible key could be used for data) that i want to identify as 'Skaperen'. would i do foo.id = 'Skaperen' or setattr(foo,'id','Skaperen')?
skaperen = id(object)
(Oct-04-2018, 01:46 AM)ichabod801 Wrote: [ -> ]skaperen = id(object)
what i want to do is store the identity with the object in a way that some code like a function or class that needs the identity can get it by having nothing more than a reference to the objecr.
Of course you do. You always want more.
storing the identity with tho object was my original goal.