How to convert a Python dict to a Class object

Alex Predescu

I am looking for a simple way to directly convert a python dict to a custom object as follows, while not breaking intellisense. The result should not be read-only, it should behave just like a new object.

d = {
    "key1": 1,
    "key2": 2
}

class MyObject(object):
    key1 = None
    key2 = None

# convert to object o

# after conversion
# should be able to access the defined props
# should be highlighted by intellisense
print(o.key1)
# conversion back to dict is plain simple
print(o.__dict__)
Adirio

Your object has no fields, just class attributes. You need to create a __init__ method, some will call it a constructor but it is not actually a constructor as understood by other languages so lets avoid calling it like that.

class MyObject:
    def __init__(self, d=None):
        if d is not None:
            for key, value in d.items():
                setattr(self, key, value)

d = {
    "key1": 1,
    "key2": 2,
}

o = MyObject(d)

Note: the above code will try to set all key-value pairs in the dict to fields in the object. Some valid keys such as "key.1" will not be valid field names (it will actually be set but you will not be able to get it with o.key.1).

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to convert dict to expressions in python

分類Dev

Why Tensorflow error: `failed to convert object of type <class 'dict'> to Tensor` happens and How can I solve it?

分類Dev

How to convert ruamel.yaml to dict with Python3?

分類Dev

Convert Class<Object> to Class<Interface>

分類Dev

Convert object to a C# class

分類Dev

How to convert multiple lines string to dict in Ansible?

分類Dev

How to convert form to class?

分類Dev

python error 'dict' object has no attribute: 'add'

分類Dev

How to use multidimensional dict in python

分類Dev

How to define class in python and inherit a COM object properties?

分類Dev

is there way to convert object into another class in ruby

分類Dev

How to clone a object to a object of Object class?

分類Dev

How not to convert an anonymous class to a regular class

分類Dev

Is there an idiomatic way to convert a Class to an Object when the class relies on an implicit?

分類Dev

How to convert an object to JSON in Angular

分類Dev

How to convert array to object in Javascript?

分類Dev

How to convert object into string in javascript?

分類Dev

How To Convert IEnumerable To one object

分類Dev

How to convert a String to Object in Flutter?

分類Dev

as.glht: Cannot convert an object of class ‘list’ to a ‘glht’ object

分類Dev

Converting Python Class Object To A DataFrame

分類Dev

Dict into Dict Python

分類Dev

TypeError: 'dict' object is not callable fix in python card game

分類Dev

How to make dict element value as list in Python

分類Dev

How to speed up intersection of dict of sets in Python

分類Dev

Convert DataFrame to dict

分類Dev

How to add object to existing class object

分類Dev

How convert this type of data <hdf5 object reference> to something more readable in the python?

分類Dev

Convert a data-table into a class object dynamically using c#

Related 関連記事

  1. 1

    How to convert dict to expressions in python

  2. 2

    Why Tensorflow error: `failed to convert object of type <class 'dict'> to Tensor` happens and How can I solve it?

  3. 3

    How to convert ruamel.yaml to dict with Python3?

  4. 4

    Convert Class<Object> to Class<Interface>

  5. 5

    Convert object to a C# class

  6. 6

    How to convert multiple lines string to dict in Ansible?

  7. 7

    How to convert form to class?

  8. 8

    python error 'dict' object has no attribute: 'add'

  9. 9

    How to use multidimensional dict in python

  10. 10

    How to define class in python and inherit a COM object properties?

  11. 11

    is there way to convert object into another class in ruby

  12. 12

    How to clone a object to a object of Object class?

  13. 13

    How not to convert an anonymous class to a regular class

  14. 14

    Is there an idiomatic way to convert a Class to an Object when the class relies on an implicit?

  15. 15

    How to convert an object to JSON in Angular

  16. 16

    How to convert array to object in Javascript?

  17. 17

    How to convert object into string in javascript?

  18. 18

    How To Convert IEnumerable To one object

  19. 19

    How to convert a String to Object in Flutter?

  20. 20

    as.glht: Cannot convert an object of class ‘list’ to a ‘glht’ object

  21. 21

    Converting Python Class Object To A DataFrame

  22. 22

    Dict into Dict Python

  23. 23

    TypeError: 'dict' object is not callable fix in python card game

  24. 24

    How to make dict element value as list in Python

  25. 25

    How to speed up intersection of dict of sets in Python

  26. 26

    Convert DataFrame to dict

  27. 27

    How to add object to existing class object

  28. 28

    How convert this type of data <hdf5 object reference> to something more readable in the python?

  29. 29

    Convert a data-table into a class object dynamically using c#

ホットタグ

アーカイブ