setattr for secondary attribute

A

Alex

I apologize for asking maybe a very trivial question.
I have a new class object A with slots. One of the slots is, for
example, object spam. Object spam, in turn, also has slots and one of
them is attribute eggs. I need to assign a new value to eggs. In other
words, I need to perform the following:

A.spam.eggs=newValue

The problem is that I have only a string s='spam.eggs' with which to
work, so I cannot write an expression written above. I tried to use
setattr:

setattr(A, s, newValue)

but this does not work. It says that object A does not have attribute
spam.eggs

How would you do it? TIA.
 
F

Fredrik Lundh

Alex said:
I apologize for asking maybe a very trivial question.
I have a new class object A with slots. One of the slots is, for
example, object spam. Object spam, in turn, also has slots and one of
them is attribute eggs. I need to assign a new value to eggs. In other
words, I need to perform the following:

A.spam.eggs=newValue

The problem is that I have only a string s='spam.eggs' with which to
work, so I cannot write an expression written above. I tried to use
setattr:

setattr(A, s, newValue)

but this does not work. It says that object A does not have attribute
spam.eggs

since "eggs" is an attribute of the "spam" attribute, you need to fetch
the latter first:

setattr(getattr(A, "spam"), "eggs")

</F>
 
B

Bengt Richter

I apologize for asking maybe a very trivial question.
I have a new class object A with slots. One of the slots is, for
example, object spam. Object spam, in turn, also has slots and one of
them is attribute eggs. I need to assign a new value to eggs. In other
words, I need to perform the following:

A.spam.eggs=newValue

The problem is that I have only a string s='spam.eggs' with which to
work, so I cannot write an expression written above. I tried to use
setattr:

setattr(A, s, newValue)

but this does not work. It says that object A does not have attribute
spam.eggs

How would you do it? TIA.
A.spam.eggs=newValue

(which would work) really means, if you break it into steps

(A.spam).eggs = newValue

which is

setattr(getattr(A, 'spam'), 'eggs', newValue)

so you just need to break 'spam.eggs' apart and plug in the pieces.
assuming exactly one dot and no spaces to worry about,

s_spam, s_eggs = s.split('.')

then

setattr(getattr(A, s_spam), s_eggs, newValue)

ought to do the job.

If you have more than one dot, e.g., s = 'spam.eggs.ham'
and eggs has a slot ham that you want to assign to, then you need to
loop on getattr for all but the last name, which you use for the setattr.
Something like (untested)

def setbydots(A, s, val):
names = s.split('.')
for name in names[:-1]:
A = getattr(A, name)
setattr(A, names[-1], val)

This should work for no dots or any number, so long as the reference chain is valid.
E.g.,
setbydots(A, 'spam.eggs', newValue)
should accomplish
A.spam.eggs=newValue
as you wanted. I hope ;-)

( posting delayed >12 hrs due to news server prob ;-/ )

Regards,
Bengt Richter
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top