getattr is returning character '>' instead of mehtod
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.
python python-2.7 getattr
add a comment |
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.
python python-2.7 getattr
add a comment |
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.
python python-2.7 getattr
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.
python python-2.7 getattr
python python-2.7 getattr
edited Jan 1 at 15:49
JJJ
29.2k147591
29.2k147591
asked Jan 1 at 15:43
bikasbikas
41
41
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996769%2fgetattr-is-returning-character-instead-of-mehtod%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 1 at 16:09
BoarGulesBoarGules
8,01121127
8,01121127
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996769%2fgetattr-is-returning-character-instead-of-mehtod%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown