Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dict and __dict__
#1
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__
Reply
#2
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__
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  __dict__ in math library akbarza 6 878 Sep-28-2023, 08:23 AM
Last Post: buran
  Sort a dict in dict cherry_cherry 4 62,810 Apr-08-2020, 12:25 PM
Last Post: perfringo
  what is the __dict__ object? aaron1989041 6 4,686 Mar-29-2018, 05:18 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020