-1

I have a class defined in feat.py

class feat:
  def __init__(self):
    print 'feat init '
    pass


  def do_something(self):
    return true

Now I am calling the following:

from feat import *
f=feat()
for i in dir(f): #feature_functions:
        i_str = str(i)
        print 'f has this attribute',  hasattr(f,i)
        print 'f has  attribute value',  getattr(f,i)

I am getting output:

feat init 
f has this attribute True 
f has attribute value > 

I tried using i_str like the following

print 'f has this attribute',  hasattr(f,i_str)
print 'f has  attribute value',  getattr(f,i_str)

I am getting the same output.

Shouldn't the output look like the following?

f has this attribute True 
f has attribute value <function do_something at 0x10b81db18>

Would appreciate any suggestion. I am using Python 2.7.

JJJ
  • 32,902
  • 20
  • 89
  • 102
bikas
  • 9
  • 1

1 Answers1

0

I made minor changes to your code to show more clearly what is going on:

from feat import *
f=feat()
for i in dir(f): #feature_functions:
        print i, 'f has this attribute',  hasattr(f,i)
        print i, 'f has  attribute value',  getattr(f,i)

And this is the output I get:

feat init 
__doc__ f has this attribute True
__doc__ f has  attribute value None
__init__ f has this attribute True
__init__ f has  attribute value <bound method feat.__init__ of <feat.feat instance at 0x0000000004140FC8>>
__module__ f has this attribute True
__module__ f has  attribute value feat
do_something f has this attribute True
do_something f has  attribute value <bound method feat.do_something of <feat.feat instance at 0x0000000004140FC8>>

Which is what we both expect. The chief difference is that the changed code isn't calling str(). I suspect you may have redefined str(), possibly in some earlier iteration of the code, and the redefinition is still lying around in your shell session's namespace. If so, starting a fresh interpreter session with only the code you present should solve the problem.

BoarGules
  • 16,440
  • 2
  • 27
  • 44