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):
def __init__(self):
self.C = C()
class C(object):
def __init__(self, foo='', bar=''):
self.foo = foo
self.bar = bar
and now i wanna do something like this:
a=A()
ext_setattr(a, 'B.C', ('a', 'b'))
Is this possible? It would also be nice if the attributes would be
created if they not exist, always implying that
objectname==obj ecttype.
Kind regards,
Andre 4 1406
On 7 Jul., 08:01, Rotlaus <rotl...@gmail. comwrote:
2 weeks ago i asked for a etended getattr() which worked really fine,
but now i would love to have a extendedsetattr () as well.
I've tried the following, but it doesn't work:
class A(object):
def __init__(self):
self.B = B()
class B(object):
def __init__(self):
self.C = C('foo')
class C(object):
def __init__(self, txt=''):
self.txt = txt
def ext_setattr(obj , attr, val):
for subattr in attr.split(".") :
obj = getattr(obj, subattr)
obj = val
>>import test a = A()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined
>>a = test.A() a.B.C.txt
'foo'
>>ext_setattr(a , 'B.C.txt', 'bar') a.B.C.txt
'foo'
What am i doing wrong?
Rotlaus schrieb:
On 7 Jul., 08:01, Rotlaus <rotl...@gmail. comwrote:
>2 weeks ago i asked for a etended getattr() which worked really fine, but now i would love to have a extendedsetattr () as well.
I've tried the following, but it doesn't work:
class A(object):
def __init__(self):
self.B = B()
class B(object):
def __init__(self):
self.C = C('foo')
class C(object):
def __init__(self, txt=''):
self.txt = txt
def ext_setattr(obj , attr, val):
for subattr in attr.split(".") :
obj = getattr(obj, subattr)
obj = val
>>>import test a = A()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined
>>>a = test.A() a.B.C.txt
'foo'
>>>ext_setattr( a, 'B.C.txt', 'bar') a.B.C.txt
'foo'
What am i doing wrong?
obj = val won't work.
You need to use a setattr(obj, name, val)
on the last attribute-name.
Diez
Diez B. Roggisch <deets <atnospam.web.d ewrites:
def ext_setattr(obj , attr, val):
for subattr in attr.split(".") :
obj = getattr(obj, subattr)
obj = val
>>import test a = A()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined
>>a = test.A() a.B.C.txt
'foo'
>>ext_setattr(a , 'B.C.txt', 'bar') a.B.C.txt
'foo'
What am i doing wrong?
obj = val won't work.
Why is this so? Shouldn't it be the same?
You need to use a setattr(obj, name, val)
on the last attribute-name.
Ok, so this works:
def ext_setattr(obj , attr, val):
attributes = attr.split('.')
for subattr in attributes[:-1]:
obj = getattr(obj, subattr)
setattr(obj, attributes[-1], val)
Andre Adrian wrote:
Diez B. Roggisch <deets <atnospam.web.d ewrites:
def ext_setattr(obj , attr, val):
for subattr in attr.split(".") :
obj = getattr(obj, subattr)
obj = val
import test a = A()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined a = test.A() a.B.C.txt
'foo' ext_setattr( a, 'B.C.txt', 'bar') a.B.C.txt
'foo'
What am i doing wrong?
obj = val won't work.
Why is this so? Shouldn't it be the same?
No, of course not!
obj = val
binds the object reffered to by val to the LOCAL name obj. That's python
101, make sure you get variables/names and scopes proper.
>You need to use a setattr(obj, name, val) on the last attribute-name.
Ok, so this works:
def ext_setattr(obj , attr, val):
attributes = attr.split('.')
for subattr in attributes[:-1]:
obj = getattr(obj, subattr)
setattr(obj, attributes[-1], val)
Yep.
Diez This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Roman Yakovenko |
last post by:
Hi. I have small problem and I don't know solution.
The problem: every class imported from some module X should not allow to be modified
( modified == adding or deleting new class attributes ).
Small explanation about what I am doing: I has complex dll written in Managed C++ for C#.
I am using "Python for .NET" and python unittest module to test my dll.
I find myself often make a mistake in attributes names. I'd like to protect my self...
|
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: Donn Ingle |
last post by:
Hi,
Here's some code, it's broken:
class Key( object ):
def __init__(self):
self.props = KeyProps()
def __getattr__(self, v):
return getattr( self.props,v )
def __setattr__(self,var,val):
|
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: Bojan Mihelac |
last post by:
Hi all - when trying to set some dynamic attributes in class, for
example:
class A:
for lang in :
exec('title_%s = lang' % lang) #this work but is ugly
# setattr(A, "title_%s" % lang, lang) # this wont work
setattr(A, "title_1", "x") # this work when outside class
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |