Posts: 4 Threads: 1 Joined: Mar 2019 In[1]: getattr(None, '') is None Traceback (most recent call last): File "C:\Users\myluo\AppData\Local\Programs\Python\Python36\lib\site-packages\IPython\core\interactiveshell.py", line 3265, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-8-dd6743d652c1>", line 1, in <module> getattr(None, '') is None AttributeError: 'NoneType' object has no attribute '' In[2]: getattr(None, '', None) is None Out[2]: True What's happend? Posts: 2,167 Threads: 35 Joined: Sep 2016 The first use of getattr returned an attribute error because an empty string was given and no default was given. The second use of getattr was also given an empty string but because a default was provided the default value was returned instead. Posts: 4 Threads: 1 Joined: Mar 2019 Mar-11-2019, 02:35 AM (This post was last modified: Mar-11-2019, 02:35 AM by MingyuanLuo.) (Mar-10-2019, 08:30 AM)Yoriz Wrote: The first use of getattr returned an attribute error because an empty string was given and no default was given. The second use of getattr was also given an empty string but because a default was provided the default value was returned instead. def getattr(object, name, default=None) The default argument has None as default value. Posts: 4,216 Threads: 97 Joined: Sep 2016 (Mar-11-2019, 02:35 AM)MingyuanLuo Wrote: The default argument has None as default value. Where are you getting that from? It's not in the documentation. The documentation describes the behavior of getattr as Yoriz did, and as you saw. Posts: 1,953 Threads: 8 Joined: Jun 2018 Mar-11-2019, 08:41 AM (This post was last modified: Mar-11-2019, 08:41 AM by perfringo.) Yoriz already gave you an answer. I add advice which may speed up learning process. If you encounter something you don't understand (or something what is unexpected) I suggest always start with built-in help and documentation. >>> help(getattr) Help on built-in function getattr in module builtins: getattr(...) getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case. (END) Documentation at python.org: 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. In ipython there is even less typing: In [1]: getattr? Docstring: getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case. Type: builtin_function_or_method I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. Posts: 4 Threads: 1 Joined: Mar 2019 (Mar-11-2019, 02:50 AM)ichabod801 Wrote: (Mar-11-2019, 02:35 AM)MingyuanLuo Wrote: The default argument has None as default value. Where are you getting that from? It's not in the documentation. The documentation describes the behavior of getattr as Yoriz did, and as you saw. I saw and understand the documentation. But I saw this in builtins.py def getattr(object, name, default=None): # known special case of getattr """ getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case. """ pass Posts: 4 Threads: 1 Joined: Mar 2019 (Mar-11-2019, 08:41 AM)perfringo Wrote: Yoriz already gave you an answer. I add advice which may speed up learning process. If you encounter something you don't understand (or something what is unexpected) I suggest always start with built-in help and documentation. >>> help(getattr) Help on built-in function getattr in module builtins: getattr(...) getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case. (END) Documentation at python.org: 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. In ipython there is even less typing: In [1]: getattr? Docstring: getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case. Type: builtin_function_or_method I saw and understand the documentation. But I saw this in builtins.py def getattr(object, name, default=None): # known special case of getattr """ getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y. When a default argument is given, it is returned when the attribute doesn't exist; without it, an exception is raised in that case. """ pass Posts: 4,216 Threads: 97 Joined: Sep 2016 (Mar-20-2019, 10:35 AM)MingyuanLuo Wrote: I saw and understand the documentation. But I saw this in builtins.py Then builtins.py is wrong, and the documentation is right, because the documentation describes how it actually works when you run it in Python. |