473,385 Members | 1,838 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,385 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 1938
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
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
Oralloy
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,...
0
jinu1996
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...

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.