Good work. For the extra credit, can you try and make one more attempt at the extra credit question.
"What is the relationship between dir(something) and the class of something" ?
Thanks Parag.
EXTRA CREDITS 5>>> class DirTest:
def test_print():
print "test"
def dir():
print "dir called"
return "testabcd"
a = DirTest()
dir(a)
Traceback (most recent call last): File "", line 1, in dir(a) TypeError: dir() takes no arguments (1 given)
class DirTest: def test_print(): print "test" def dir(self): print "dir called" return "testabcd"
a = DirTest()
dir(a) dir called
Traceback (most recent call last): File "", line 1, in dir(a) TypeError: dir() must return a list, not str
class DirTest: def test_print(): print "test" def dir(self): print "dir called" return ["testabcd"]
a = DirTest()
dir(a) dir called ['testabcd']
Thanks Parag. dir(class) results in call to dir function of the class.
For example:
EXTRA CREDITS 5>>> class DirTest:
def test_print():
print "test"
def __dir__(self):
print "__dir__ called"
return ["testabcd"]
>>> a = DirTest()
>>> dir(a)
__dir__ called
['testabcd']
