setattr and getattr, when to use?

M

maestro

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?
 
J

Jason Scheirer

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.
 
J

Jason Scheirer

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.
 
S

Steven D'Aprano

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.
 
B

Bruno Desthuilliers

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" ???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top