Adding attribute to object

M

marco

hi

I've got a class "myClass" ... and i CAN'T EXTENDS this class!
But i can add NEW methods to "myClass", like that :

method = new.instancemethod( myNewMethod , None, myClass)
myClass.__dict__[method.__name__] = method

i'd like to add NEW attributs to "myClass" ...
i'm pretty sure it's possible (with "get/set-attr") ... but i don't know how
to do that
in fact, i'd like to wrap an newAttribut to an existingAttribut ...
can anyone drop me 3 lines on how to do that ?
 
J

Jay O'Connor

marco said:
hi

I've got a class "myClass" ... and i CAN'T EXTENDS this class!
But i can add NEW methods to "myClass", like that :

method = new.instancemethod( myNewMethod , None, myClass)
myClass.__dict__[method.__name__] = method

i'd like to add NEW attributs to "myClass" ...
i'm pretty sure it's possible (with "get/set-attr") ... but i don't know how
to do that
in fact, i'd like to wrap an newAttribut to an existingAttribut ...
can anyone drop me 3 lines on how to do that ?

I confess I'm thinking that if you are dynamically adding variables (or
methods) to a class (not instance variables and methods) then
something is odd with your design
 
J

John Roth

marco said:
hi

I've got a class "myClass" ... and i CAN'T EXTENDS this class!
But i can add NEW methods to "myClass", like that :

method = new.instancemethod( myNewMethod , None, myClass)
myClass.__dict__[method.__name__] = method

i'd like to add NEW attributs to "myClass" ...
i'm pretty sure it's possible (with "get/set-attr") ... but i don't know how
to do that
in fact, i'd like to wrap an newAttribut to an existingAttribut ...
can anyone drop me 3 lines on how to do that ?

I'm not at all sure what you're trying to do, since new. simply
isn't necessary.

if you have a class

class FooBar(object):
pass

and you want to add an attribute to it, just do it.

FooBar.newAttr = "Hi, There."

Of course, Jay O'Connor is also correct - why would you
want to do that? (There are reasons why you might, it just
doesn't seem like a novice type thing to do.)

John Roth
 

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
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top