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=new Value
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. 4 1477
Alex wrote: 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=new Value
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>
Alex wrote: 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=new Value
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>
Great! Thanks, it works (of course).
Great! Thanks, it works (of course). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Eric |
last post by:
Slightly off topic, i know, but here goes:
I'm trying to xlate a module of mine to C++. Only problem is, it makes
heavy use of "setattr". Anyone know a straightforward way to do
"setattr" in C++ ?
thanks,
Eric
|
by: Gerson Kurz |
last post by:
I stumbled across this (while using my homebrewn enum class):
class test:
pass
instance = test()
setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)
I would've expected some kind of error message here when calling
setattr(); after all, its not a regular attribute? Plus, documentation
|
by: kramb64 |
last post by:
I'm trying to use setattr inside a module.
From outside a module it's easy:
import spam
name="hello"
value=1
setattr(spam, name, value)
But if I want to do this inside the module spam itself, what I've to
pass to setattr as first argument?
|
by: Alex |
last post by:
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...
|
by: Paulo da Silva |
last post by:
Hi!
In a class C, I may do setattr(C,'x',10).
Is it possible to use getattr/setattr for variables not inside
classes or something equivalent? I mean with the same result as
exec("x=10").
Thanks.
| |
by: Nathan Harmston |
last post by:
Hi,
I m trying to implement an object which contains lazy" variables. My
idea is to alter the getattr and the setattr methods. However I keep
on getting a recursion error.
My idea is that the lazy variable can be stored in a variety of
places, Database, PyTables etc. The lazy variable is a large variable
and so I dont want to hold it in memory all of the time, I d rather
just get it when needed and then store it for future work. Most...
|
by: james_027 |
last post by:
hi,
My main purpose for using setattr(object, attr, value) for assign
values from a dict that has some keys that may not be present on some
object's attibute, which makes it work for me. My problem is dealing
with read only attribute like sample_attribute =
property(f_get=_get_sample_attribute). what approach should I use? Or
is my first approach In the first place right?
Thanks
|
by: Rotlaus |
last post by:
2 weeks ago i asked for a etended getattr() which worked really fine,
but now i would love to have a extended setattr() as well.
Lets assume i have some classes:
class A(object):
def __init__(self):
self.B = B()
class B(object):
|
by: maestro |
last post by:
Why are these functions there? Is it somehow more idiomatic to use
than to do obj.field ?
Is there something you can with them that you can't by obj.field
reference?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |