Python Forum

Full Version: dict and __dict__
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here, may i know why when i use
Peter.dict
the following error will occur

Error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'Celsius' object has no attribute 'dict'
But when i use
Peter.__dict__
It will work. What's the difference? Many thanks.

class Celsius:
    def __init__(self, temperature = 0):
        self.temperature = temperature

    def to_fahrenheit(self):
        return (self.temperature * 1.8) + 32

Peter = Celsius()
Peter.temperature = 37
Peter.__list__
Because your object has no attribute dict when every object in python has attribute __dict__ which maps all objects attribute to it's value.

More detaild in doc: https://docs.python.org/3/library/stdtyp...t.__dict__