Added snippet for using getattr. getattr, hasattr, and setattr can be used to write generic, reusable code.

This commit is contained in:
Donne Martin 2015-01-23 16:29:19 -05:00
parent cfa84449b9
commit a7262a50a2

View File

@ -1,4 +1,5 @@
from nose.tools import assert_equal
from nose.tools import raises
class TestUtil():
@ -21,3 +22,18 @@ class TestUtil():
assert_equal(isinstance(7, (int)), True)
assert_equal(isinstance(7.5, (int, float)), True)
assert_equal(isinstance('foo', (int, float, str)), True)
def test_attributes(self):
"""Functions getattr, hasattr, setattr can be used to write generic,
reusable code. Objects in python have both attributes and methods
"""
# getattr will throw an AttributeError exception if the attribute
# does not exist
getattr('foo', 'split')
@raises(Exception)
def test_attributes_fail(self):
return getattr('foo', 'bar')