mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
Added snippet for using getattr. getattr, hasattr, and setattr can be used to write generic, reusable code.
This commit is contained in:
parent
cfa84449b9
commit
a7262a50a2
|
@ -1,4 +1,5 @@
|
||||||
from nose.tools import assert_equal
|
from nose.tools import assert_equal
|
||||||
|
from nose.tools import raises
|
||||||
|
|
||||||
|
|
||||||
class TestUtil():
|
class TestUtil():
|
||||||
|
@ -21,3 +22,18 @@ class TestUtil():
|
||||||
assert_equal(isinstance(7, (int)), True)
|
assert_equal(isinstance(7, (int)), True)
|
||||||
assert_equal(isinstance(7.5, (int, float)), True)
|
assert_equal(isinstance(7.5, (int, float)), True)
|
||||||
assert_equal(isinstance('foo', (int, float, str)), 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')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user