Introduction To Python - Learn Python the hard way v2.0
Section: Exercise 39: Doing Things To Lists
Activity: Please submit links to your blog posts related to this activity
http://twitter.com/prasadjgd submitted this response ( Exercise 39: Doing Things To Lists ) about Nov 21, 2011
0 voteup image
Reviews
This section contains reviews provided by the community for the activity response specified at the top of the page. Please use the form at the end to provide a constructive review of the activity.

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']

Please login to provide a review of this activity response