473,395 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Adding attribute to object

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 ?


Jul 18 '05 #1
2 2442
marco wrote:
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
Jul 18 '05 #2

"marco" <ma********@ctrceal.caisse-epargne.fr> wrote in message
news:bq**********@s1.read.news.oleane.net...
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

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
9
by: Michelle | last post by:
I have a div that is initially empty. Clicking on a button will add some text boxes and other controls so the user can add additional records. In IE all works fine but in Netscape 7.0 when I add...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
11
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .......
3
by: vikram | last post by:
How can i add a user define attribute to each checkbox item in a checkbox list control. I want to add a attribute "Tag". Is it possible
3
by: Andy | last post by:
Hello Guys: What am I doing wrong with this code? I can't seem to get it to simply add an attribute to my node. The node already exists. I am simply opening the XMLDocument and creating one...
9
by: Mike | last post by:
I was messing around with adding methods to a class instance at runtime and saw the usual code one finds online for this. All the examples I saw say, of course, to make sure that for your method...
5
by: Bob B. | last post by:
I've been playing with descriptors lately. I'm having one problem I can't seem to find the answer to. I want to assign a descriptor to a list of attributes. I know I should be able to add these...
5
by: Neal Becker | last post by:
After spending the morning debugging where I had misspelled the name of an attribute (thus adding a new attr instead of updating an existing one), I would like a way to decorate a class so that...
1
by: libish | last post by:
can any one help me in adding type attribute to a textbox object??? here what i need is to add an attribute ie. alphanumeric,numeric,password etc.... ie a text box after adding this attribute...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.