Adding/removing instance members

  • Thread starter Ladvánszky Károly
  • Start date
L

Ladvánszky Károly

I understand instance members can be added to an object in a dynamic manner:

class Cc:
pass

oCc=Cc()
oCc.x=10 # oCc now has a new member

What is the way to remove these instance members?
Is it possible to add a new member whose name is given by a string?

Thanks on any help,

Károly
 
P

Peter Otten

Ladvánszky Károly said:
I understand instance members can be added to an object in a dynamic
manner:

class Cc:
pass

oCc=Cc()
oCc.x=10 # oCc now has a new member

What is the way to remove these instance members?
Is it possible to add a new member whose name is given by a string?

You can delete an attribute like so:

del c.x

Use setattr/getattr/delattr() to set, get or delete attributes known only by
their name:
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: C instance has no attribute 'x'

Peter
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top