Why does this not work?

A

alain

I tried the following:

myobj=object()
myobj.newattr=5

results in:


Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'object' object has no attribute 'newattr'

Any idea?


Alain
 
B

Bart Van Loon

I tried the following:

myobj=object()
myobj.newattr=5

results in:


Traceback (most recent call last):
File "<interactive input>", line 1, in ?
AttributeError: 'object' object has no attribute 'newattr'

Any idea?

I think it's because... object has no attribute 'newattr'

what else is there left to say?

try:

myobj=object()
print dir(myobj)

does that contain 'myattr'?
 
A

alain

I think it's because... object has no attribute 'newattr'

what else is there left to say?

try:

myobj=object()
print dir(myobj)

does that contain 'myattr'?

--
groetjes,
BBBart

"To make a bad day worse, spend it wishing for the impossible." -Calvin


What about this:
class Object(object):pass
myobj=Object()
myobj.newattr=5

and it works !!!
Python allows the dynamic creation of attributes for an instance of an
object.

Alain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top