if understanding of python data model correct, both classes , class instances have associated __dict__
objects contain attributes. however, i'm little confused why class instances, such instances of str
example, don't have __dict__
attribute.
if create custom class:
class foo: def __init__(self): self.firstname = "john" self.lastname = "smith"
then can instance variables saying:
>>> f = foo() >>> print(f.__dict__) {'lastname': 'smith', 'firstname': 'john'}
but if try same instance of built-in str
, get:
>>> s = "abc" >>> print(s.__dict__) traceback (most recent call last): file "<stdin>", line 1, in <module> attributeerror: 'str' object has no attribute '__dict__'
so, why don't instances of str
have __dict__
attribute?
instances of types defined in c don't have __dict__ attribute default.
Comments
Post a Comment