469,609 Members | 1,994 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

setattr and getattr, when to use?

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?
Aug 23 '08 #1
4 10134
On Aug 22, 8:50*pm, maestro <notnorweg...@yahoo.sewrote:
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?
You can generate them dynamically from strings. In some cases you
don't know until runtime what attributes you want to pull:

def show_insides(obj):
for attr in dir(obj):
print "Attribute %r: %r" % (attr, getattr(obj, attr))

class hello(object):
a = 1
b = 2

class goodbye(object):
c = 1
d = 500
print show_insides(hello)
(...15 builtins...)
Attribute 'a': 1
Attribute 'b': 2

print show_insides(goodbye)
(...15 builtins...)
Attribute 'c': 1
Attribute 'd': 500

In this case, you can see that we pull the attributes of an object
using dir(), which yields a list of strings, then pull each attribute
we discover.
Aug 23 '08 #2
On Aug 22, 10:17*pm, Jason Scheirer <jason.schei...@gmail.comwrote:
On Aug 22, 8:50*pm, maestro <notnorweg...@yahoo.sewrote:
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?

You can generate them dynamically from strings. In some cases you
don't know until runtime what attributes you want to pull:

def show_insides(obj):
* for attr in dir(obj):
* * print "Attribute %r: %r" % (attr, getattr(obj, attr))

class hello(object):
* *a = 1
* *b = 2

class goodbye(object):
* *c = 1
* *d = 500

print show_insides(hello)
(...15 builtins...)
Attribute 'a': 1
Attribute 'b': 2

print show_insides(goodbye)
(...15 builtins...)
Attribute 'c': 1
Attribute 'd': 500

In this case, you can see that we pull the attributes of an object
using dir(), which yields a list of strings, then pull each attribute
we discover.
Might I add: avoid doing this if you don't have to. obj.field is the
way to go about getting your object attributes 95% of the time. The 5%
of your time when you are doing metaprogramming or other abuses of the
object system are when you use get/setattr.
Aug 23 '08 #3
On Fri, 22 Aug 2008 20:50:17 -0700, maestro wrote:
Why are these functions there? Is it somehow more idiomatic to use than
to do obj.field ?

Heavens no!!! Using setattr and getattr is *less* idiomatic.

Is there something you can with them that you can't by obj.field
reference?
Absolutely. Consider:

class Foo(object):
pass

foo = Foo()
setattr(foo, "x y", 1)
print getattr(foo, "x y")

That's probably an abuse of setattr/getattr, but there are times where
you might not know the name of the attribute until runtime, so you can't
write foo.attr since you don't know what to write in place of attr.
That's where setattr and getattr (as well as delattr and hasattr) come
into play.

--
Steven
Aug 23 '08 #4
Jason Scheirer a écrit :
(snip)
The 5%
of your time when you are doing metaprogramming or other abuses of the
object system are when you use get/setattr.
What makes you label "metaprogramming" and get/setattr as "abuses" ???
Aug 26 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Alex | last post: by
8 posts views Thread by Steven D'Aprano | last post: by
10 posts views Thread by Paulo da Silva | last post: by
reply views Thread by Nathan Harmston | last post: by
6 posts views Thread by Donn Ingle | last post: by
4 posts views Thread by Rotlaus | last post: by
reply views Thread by Solution2021 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.