is there any way to get the object from its property?

FLAW

I want to list the objects by its attribute, and get it with only the attributes that is in the list.

class foo:
    def __init__(self,id):
        self.id=id

a=foo(0)
b=foo(1)

ids=[a.id,b.id]

can I refer to a with only having ids ?

and if it is not possible this way, how can I ?

Federico Baù

User a dictionary:

class foo:
    def __init__(self,id):
        self.id=id


a=foo(0)
b=foo(1)

ids={a.id:a, b.id:b}
print(ids[0])

An example without a dictionary

NOTE: This may be better achieved using a Meta-programming in Python, and your question may seem that can have an actual real world usage when creating Python Packages, Frameworks etc.

Still, in a clumsy way it does achieve this.

import random

class foo:
    def __init__(self,id):
        self.id=id


def create_counter():
    count = 0
    def internal():
        nonlocal count
        count += 1
        return count
    return internal

counter = create_counter()

def create_id():
    """Generate random id, uses a stateles Closure to keep track of counter"""

    id_ = None
    name = 'class_'
    id_gen = str(hex(random.randrange(1000)))       
    id_ =  name + str(counter()) + "_" + id_gen[2:]        

    return id_


def change_name_ref(inst_obj):
    """Change Instance Name to Instance ID"""
    inst_obj.__name__ = inst_obj.id



a = foo(create_id()) # --> Assign a radnom Id 
b = foo(create_id())
c = foo('class_1_15b')

change_name_ref(a)
change_name_ref(b)
change_name_ref(c)

ids = [a, b, c]


def get_instance(inst_list, target):
 
    for idx, id_ in enumerate(inst_list):
        if id_.__name__ == target:
            inst = inst_list[idx]
            print(f'Here The Class instance {inst}, ID: {inst.id}')


value = get_instance(ids, 'class_1_15b')

# Here The Class instance <__main__.foo object at 0x7f6988f016d0>, ID: class_1_15b

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is there a way to get Class from Any (when Any is a instance of a object)?

Any way to get the definition of a property from the code itself?

Efficient way to get an object from collection by value of one of its fields

Efficient way to get unique objects from array of objects, by object property?

Get property from an object

Get a property from object

How to get index of object by its property in JavaScript?

Any way to check an object if its breeze entity object?

Get property of a Kotlin reflected object with type Any

How to get the object property and its property value if the property value is not null then transform the object property?

Is there any way to retrieve the chart object by its id in dc.js

Is there any way to get the guild object a command is run it?

Is there any way to get the name of an Knockout observable object?

Find object by its property in array of objects with AngularJS way

A way to access a property without knowing its path in a nested js object

Get the highest date in a List of object and get another property of its class

Is there any way to get offsetheight of an element by its Class Name

In Elixir, is there any way to get a module to list its functions?

is there any way to get form elements and its related values

Java streams on List<Object> get a property of any object matching filter()

Is there any way to validate counts of different object types linked by same property?

Is there any safe way to check the deep of object's property?

Is there any way to get the xpath of element stored following page object model from a separate file?

Is there any way I can get a sorted array of key from key value of the object

Is there any way to get data about a query from a query object in Firebase Firestore libraries?

Java - Easiest way to get single property from each object in a list/array?

Is there any way to stop a Stream.generate from its Lambda closure?

Is there any way to read a member from a list based on its content?

Get reference to Thread Object from its ID