Posts: 4,220
Threads: 97
Joined: Sep 2016
Output: >>> x.name = 'Fred'
>>> fred = object()
>>> fred.name = 'Fred'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'name'
>>> class Thing(object):
... pass
...
>>> george = Thing()
>>> george.name = 'George'
>>> george.name
'George'
So, something in the object class prevents attributes being assigned on the fly. I'm not worried about that. What confuses me is the Thing class, which inherits from object without overriding anything. Why doesn't it inherit the blocking of attributes being assigned on the fly?
Posts: 12,026
Threads: 485
Joined: Sep 2016
May-01-2017, 04:14 PM
(This post was last modified: May-01-2017, 04:20 PM by Larz60+.)
Seems to me you should have received this error immediately:
Error: >>> x.name = 'Fred'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>>
here, was x already declared
>>> fred = object()
>>> type(fred)
<class 'object'>
>>> fred.name = 'Fred'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'object' object has no attribute 'name'
>>> class Thing(object):
... pass
...
>>> type(Thing)
<class 'type'>
>>> george = Thing()
>>> type(george)
<class '__main__.Thing'>
>>> george.name = 'George'
>>> type(george.name)
<class 'str'>
Posts: 566
Threads: 10
Joined: Apr 2017
May-01-2017, 04:16 PM
(This post was last modified: May-01-2017, 04:19 PM by volcano63.)
object does not have __dict__ attribute, thus its instantiations can't be updated
In [1]: dir(object)
Out[1]:
['__class__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__le__',
'__lt__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__']
(May-01-2017, 04:14 PM)Larz60+ Wrote: Seems to me you should have received this error immediately:
Error: >>> x.name = 'Fred'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
>>>
here, was x already declared
What was the type of your x
In [4]: class X(object):
...: pass
...:
In [5]: x = X()
In [6]: x.name = 'Fred'
In [7]: '__dict__' in dir(X)
Out[7]: True
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 4,220
Threads: 97
Joined: Sep 2016
Sorry, x was from previous messing around, it's not relevant to the question.
(May-01-2017, 04:16 PM)volcano63 Wrote: object does not have __dict__ attribute, thus its instantiations can't be updated
Okay, but then why does Thing have a dict, if Thing just inherits from object?
Posts: 3,458
Threads: 101
Joined: Sep 2016
Maybe object is a special... object?
I tried looking it up, and the tutorial doesn't actually ever show inheriting from object... https://docs.python.org/3/tutorial/classes.html
After googling some more, old-style classes don't exist in python 3, so class Something(object): is exactly the same as class Something: now.
Posts: 566
Threads: 10
Joined: Apr 2017
(May-01-2017, 04:38 PM)ichabod801 Wrote: Okay, but then why does Thing have a dict, if Thing just inherits from object? I presume that is the way object.__new__ is implemented.
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
Posts: 8,156
Threads: 160
Joined: Sep 2016
May-01-2017, 05:01 PM
(This post was last modified: May-01-2017, 05:01 PM by buran.)
(May-01-2017, 04:38 PM)ichabod801 Wrote: Okay, but then why does Thing have a dict, if Thing just inherits from object?
According to docs, The implementation adds a few special read-only attributes to several object types, where they are relevant. Some of these are not reported by the dir() built-in function.
__dict__ is one of these.
|