472,371 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

extend getattr()

Hello,

lets assume i have some classes:

class A(object):
def __init__(self):
b = B()

class B(object):
def __init__(self):
c = C()

class C(object):
def __init__(self):
pass

and now i wanna do something like this:

a=A()
c=getattr(a, 'b.c')

I know this doesn't work, but what can i do to get this or a similar
functionality to get it work for this sample and for even more nested
classes?

Kind regards,

Andre
Jun 27 '08 #1
3 1877
Rotlaus wrote:
Hello,

lets assume i have some classes:
[...]

a=A()
c=getattr(a, 'b.c')

I know this doesn't work, but what can i do to get this or a similar
functionality to get it work for this sample and for even more nested
classes?
Just recursively apply the getattr(), like this:

class A(object):
def __init__(self):
self.b = B()

class B(object):
def __init__(self):
self.c = C()

class C(object):
def __init__(self):
pass

def ext_getattr(obj, attr):
for subattr in attr.split("."):
obj = getattr(obj, subattr)
return obj

a=A()
c = ext_getattr(a, 'b.c')

-- Gerhard

Jun 27 '08 #2
Le Thursday 26 June 2008 13:06:53 Rotlaus, vous avez écrit*:
Hello,

lets assume i have some classes:

class A(object):
def __init__(self):
b = B()

class B(object):
def __init__(self):
c = C()
note you're just defining some local variables here, should be self.b = B()
and self.c = C().
class C(object):
def __init__(self):
pass

and now i wanna do something like this:

a=A()
c=getattr(a, 'b.c')

I know this doesn't work, but what can i do to get this or a similar
functionality to get it work for this sample and for even more nested
classes?
You could do it manually:

c = getattr(getattr(a, 'b'), 'c')

or make it automatic:

def get_dotted_attr (obj, dotted_attr) :
for attr in dotted_attr.split('.') :
obj = getattr(obj, attr)
return obj

a = A()
print 'a.b.c = %s' % get_dotted_attr(a, 'b.c')

--
Cédric Lucantis
Jun 27 '08 #3
On Jun 26, 7:39 am, Cédric Lucantis <o...@no-log.orgwrote:
Le Thursday 26 June 2008 13:06:53 Rotlaus, vous avez écrit :
Hello,
lets assume i have some classes:
class A(object):
def __init__(self):
b = B()
class B(object):
def __init__(self):
c = C()

note you're just defining some local variables here, should be self.b =B()
and self.c = C().
class C(object):
def __init__(self):
pass
and now i wanna do something like this:
a=A()
c=getattr(a, 'b.c')
I know this doesn't work, but what can i do to get this or a similar
functionality to get it work for this sample and for even more nested
classes?

You could do it manually:

c = getattr(getattr(a, 'b'), 'c')

or make it automatic:

def get_dotted_attr (obj, dotted_attr) :
for attr in dotted_attr.split('.') :
obj = getattr(obj, attr)
return obj

a = A()
print 'a.b.c = %s' % get_dotted_attr(a, 'b.c')
FYI, this feature will exist in operator.attrgetter from Python 2.6,
i.e. you'll be able to say attrgetter('b.c')(a).

George
Jun 27 '08 #4

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

Similar topics

3
by: Johnny | last post by:
Hi, I wonder what is the difference between the built-in function getattr() and the normal call of a function of a class. Here is the details: getattr( object, name) Return the value of...
8
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>>...
13
by: Pierre | last post by:
Hi, Sorry in advance, english is not my main language :/ I'd like to customize the result obtained by getattr on an object : if the object has the requested property then return it BUT if the...
4
by: Hole | last post by:
Hi There! I'm trying to use Zope and the product OpenFlow. I got the following error while I was using the built-in function getattr() to retrieve an OpenFlow object: attribute name must be...
4
by: Emin | last post by:
Dear experts, I got some unexpected behavior in getattr and copy.deepcopy (see transcript below). I'm not sure if this is actually a bug in copy.deepcopy or if I'm doing something too magical...
3
by: Jm lists | last post by:
Hello, Since I can write the statement like: Test whether a path is a directory Why do I still need the getattr() func as below? Test whether a path is a directory
1
by: Sergio Correia | last post by:
This works: # Module spam.py import eggs print getattr(eggs, 'omelet')(100) That is, I just call the function omelet inside the module eggs and evaulate it with the argument 100.
3
numberwhun
by: numberwhun | last post by:
Hello everyone! I am presently going through the "Dive Into Python" tutorial which I obtained from their website. No, this is not for any class, I am self-learning the Python language. I am in...
8
by: Gregor Horvath | last post by:
Hi, class A(object): test = "test" class B(object): a = A() In : B.a.test
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.