Skip to content

Class built-in attribute dir fails #803

@jeielefeld

Description

@jeielefeld

I'm using winPython 3.7.4 with spider IDE
The following code will will result in an error:

s = 'abc'
len(dir(s))

TypeError: 'str' object is not callable

also

i = 123
len(dir(i))

TypeError: 'str' object is not callable

--

Is the built-in attribute dir() somehow converted to string?

I am doing the code exercise in the book 'Python in Easy Steps' 2nd edition pp120 and 121
item #6 (pp121) in the code fails with the same error or TypeError: 'str' object is not callable

as such i did two simple tests with very simple code listed at the begging of this letter.
They also failed.

Here is the code provided by the author:

file: Bird.py
class Bird :

'''A base class to define bird properties.'''

count = 0

def __init__( self , chat ) :

	self.sound = chat
	Bird.count += 1

def talk( self ) :

	return self.sound

--
file: address.py
from Bird import *

chick = Bird( 'Cheep, cheep!' )
chick.age = '1 week'

print( '\nChick Says:' , chick.talk() )
print( 'Chick Age:' , chick.age )

chick.age = '2 weeks'
print( 'Chick Now:' , chick.age )

setattr( chick , 'age' , '3 weeks' )

print( '\nChick Attributes...' )
for attrib in dir( chick ) : # this where it fails
if attrib[0] != '_' :
print( attrib , ':' , getattr( chick , attrib ) )

delattr( chick , 'age' )
print( '\nChick age Attribute?' , hasattr( chick , 'age' ) )

--
JohnB

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions