There is a class: class Class1 ():

value1 = {"value1": "v"} value2 = {"value2": "v"} 

Next class: class Class2 (Class1):

 value3 = "value3" value4 = {Class1} - тут хочу положить value1 и value2 в словарь 

Then, in the test I call Class2:

 t = Class2() 

As a result, t is equal to:

 {"value3": "value3", {<class "путь-к-классу.Class1">}} 

Tell me how to get?

 {"value3": "value3", {{"value1": "v"}, {"value2": "v"}}} 

In the answer, besides the mentioned values, there are also such parameters as:

  mappingproxy, "init": <function Class1.__init__ at 0x7f9a88d21a60>, "module": "path_to_class", "_meta": <meta_data>, "doc": null 

It would be great to avoid their appearance.

    2 answers 2

    In python, classes have an attribute __dict__ or a method vars(obj) they return all variables of OBJECT, it means, if you declare your variables in init as self.value1' и 'self.value2 , then when creating an object of class Class1 for example :

     'a = Class1() print( a.__dict__ ) # или vars() #вывод {'value1' : {'value1' : 'v'}, 'value2' : {'value2' :'v'}` 

    To access value1, write: a. dict [0] or a. dict ['value1']

    In general, I do not understand the question for accessing the fields is the operator "." To access a home field or class method, you need to write it in a dot, for example: a.value1 returns {'value1': 'v'}

    • To get the result, just like you have in your desired make value4 = Class1().__dict__', но перед этим не забудьте сделать у класса поля полями класса, самый простой способ в конструкторе напишите: self.value1 = {"value1": "v" } `and the same for value2 - NEStenerus nester
    • I'm not sure, but maybe vars (Class1) will return the variables you need without the above amendment, test - NEStenerus nester
    • class Class1: def init_ (self): self.value1 = {"value1": "v"} self.value1 = {"value1": "v"} value4 = Class1 () ._ dict Result: value4 = [{Class1. _dict_}] TypeError: unhashable type: 'mappingproxy' what is the error? - Dmitriy Zholudev 8:59 pm
    • I figured out why there was an error - I had to remove the array. Now I get things like mappingproxy, " init ": <function Class1 .__ init__ at 0x7f9a88d21a60>, " module ": "path_to_class", "_meta": <meta_data>, " doc ": null} - Dmitriy Zholudev
    • BUT, just like that print (a. () __ dict__) # or vars (a ()) - Dmitriy Zholudev

    You probably fix this construction for CLASS. But for an object, if you execute this code, everything will work: a = Class1() print(a.__dict__) In the first line you create an object and, for the second, call the method dict which returns only variables without any (note that in the second line after the object "a", parentheses are not needed, because this is the object of the class, but if you call Class1.__dict__ then get exactly what you received with the extra fields docs, meta and etc.