How to return new Object every time by calling same dict value in Python?
NickName:Ugnius Malūkas Ask DateTime:2018-09-19T15:31:38

How to return new Object every time by calling same dict value in Python?

I am trying to set class definition for dictionary element that every time by getting dict element new object should appear.

Example:

types = {}
types[first_type] = FirstType()
types[second_type] = SecondType()

And by setting types[first_type] I have to get new FirstType():

some_var = new types[first_type] # this is illegal statement.

How can I achieve this in Python?

Copyright Notice:Content Author:「Ugnius Malūkas」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/52400483/how-to-return-new-object-every-time-by-calling-same-dict-value-in-python

Answers
iBug 2018-09-19T07:33:35

Try making them into \"object generators\" themselves.\n\ntypes = {}\ntypes[first_type] = FirstType\ntypes[second_type] = SecondType\n\n\nThen you can get a new object each time by calling that that point.\n\nsome_var = types[first_type]()\n",


More about “How to return new Object every time by calling same dict value in Python?” related questions

How to return new Object every time by calling same dict value in Python?

I am trying to set class definition for dictionary element that every time by getting dict element new object should appear. Example: types = {} types[first_type] = FirstType() types[second_type] =

Show Detail

How to return a constant value, not creating a new object every time

I have a function used to populate data into map public final class ConfigurationService { public final RMap populateCache(RedissonClient client) { RMap map = client.getMap("Config&qu...

Show Detail

Does singleton class member function return same object every time?

object Someclass { fun createEmployee(): Employee { return Employee("john", "1221") } } data class Employee(val name: String, number: String) "When i call Someclass.createEmployee() doe.

Show Detail

Mock same method with different return value called twice in a function in python

I have a method to be tested using pytest. The method is calling same database model twice. def function: ids = database_model.object.filter(user="user1").filter(group="admin").values_list(ids,

Show Detail

add new key value for every dict of dict with list of dict

I have dict of dict: a = { 'a': {'id': 1}, 'b': {'id': 1}, 'c': {'id': 1} } And list of dict: b = [{'word': 'foo'}, {'word&#x

Show Detail

Same dict object in List of dict Python

Why in Python, use of n * [dict()] to create nested dictionary causes same dictionary object? >>> d = [(111, 2222), (3333, 4444), (555, 666)] >>> d [(111, 2222), (3333, 4444), (5...

Show Detail

assigning value in python dict (copy vs reference)

I understand that in python every thing, be it a number, string, dict or anything is an object. The variable name simply points to the object in the memory. Now according to this question, >>

Show Detail

Why can a Python dict have multiple keys with the same hash?

I am trying to understand the Python hash function under the hood. I created a custom class where all instances return the same hash value. class C: def __hash__(self): return 42 I j...

Show Detail

Why can a Python dict have multiple keys with the same hash?

I am trying to understand the Python hash function under the hood. I created a custom class where all instances return the same hash value. class C: def __hash__(self): return 42 I j...

Show Detail

Why can a Python dict have multiple keys with the same hash?

I am trying to understand the Python hash function under the hood. I created a custom class where all instances return the same hash value. class C: def __hash__(self): return 42 I j...

Show Detail