0

I have got a beginner problem. I would like to get an attribute from an instance. It work fine when i use getattr with 2 separeted arguments (object and the name of the attribute).

But I read that it's possible to just use one argument (like this : obj.attr). When I try it, I get an error : getattr expected at least 2 arguments, got 1. Why does it not work ?

class test :
    def __init__ (self) :
        self.name = "a"

inst = test()

print getattr (inst, "name")
print getattr (inst.name)

a
# Error: getattr expected at least 2 arguments, got 1
# Traceback (most recent call last):
#   File "<maya console>", line 8, in <module>
# TypeError: getattr expected at least 2 arguments, got 1 #
David Ansermot
  • 6,052
  • 8
  • 47
  • 82
Morgan
  • 589
  • 9
  • 20
  • 3
    ...because `getattr (inst.name)` only passes one argument, instead of two? What attribute of `'a'` were you hoping to get? – jonrsharpe Apr 17 '15 at 12:40
  • But if inst.name is equal only to only 1 argument, why there is write in the getattr part of this website (https://docs.python.org/2/library/functions.html#getattr) that : " getattr(x, 'foobar') is equivalent to x.foobar " ? – Morgan Apr 17 '15 at 12:48
  • Because it is! `getattr(inst, 'name')` is equivalent to **`inst.name`**, but note that that is **not** the same as `getattr(inst.name)` (which doesn't make sense; you're already accessing the attribute, so what are you hoping to get?) – jonrsharpe Apr 17 '15 at 12:49
  • ok ^^. I understand where I was wrong. Thanks for the help ! – Morgan Apr 17 '15 at 12:59

2 Answers2

3

getattr() can be used to implement attribute access using a dynamic attribute name. You use it when you do not know the attribute name up front.

When you us inst.name in your code, you know the attribute name up front; you just typed it in, so Python knows you are trying to use the attribute name name here. Python can parse that expression and when the code runs, the attribute name is looked up on the object referenced by inst and a value can be produced.

But if you wanted to use a variable instead, containing the name of the attribute, then using getattr() makes sense:

attribute_name = 'name'
attribute_value = getattr(inst, attribute_name)

Here getattr() takes the object on which to look up the attribute, and a string that denotes what attribute to get for you.

Your error then lies in that you did not provide that second argument. You gave it an object as the first argument (inst.name, which in this case is a string object with the value 'a'), but not a second argument. I doubt you wanted to look up an attribute on the string 'a', however. You wanted 'a' itself, which you already have, so you can just remove the getattr() function call altogether:

getattr(inst, 'name')  # returns 'a'
inst.name              # also returns 'a'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

It doesn't work because getattr expected at least 2 arguments.

Also you can simply get your attribute without getattr:

>>> inst.name
'a'

So there is no need to use getattr!

getattr(object, name[, default])

Return the value of the named attribute of object. name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.

Community
  • 1
  • 1
Mazdak
  • 105,000
  • 18
  • 159
  • 188