python: Unable to use eval() to run object methods [duplicate]
This question already has an answer here:
TypeError: generatecode() takes 0 positional arguments but 1 was given
1 answer
I'm trying to use eval()
to run dynamic code, but it seems to reject object-based methods:
def GetHelp():
return 'Help'
class MyClass:
def __init__(self):
self.child = 5
def GetChild():
return self.child
x = eval('Get{}()'.format('Help'))
print(x)
obj = MyClass()
y = eval('obj.Get{}()'.format('Child'))
print(y)
The above code gives me:
Help
Traceback (most recent call last):
File "/Users/kakyo/Desktop/0.Dev/playground/python/hello_eval.py", line 14, in <module>
y = eval('obj.Get{}()'.format('Child'))
File "<string>", line 1, in <module>
TypeError: GetChild() takes 0 positional arguments but 1 was given
I tried to add obj to locals
:
y = eval('obj.Get{}()'.format('Child'), {'obj': obj})
but got the same error.
python built-in
marked as duplicate by deceze♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
TypeError: generatecode() takes 0 positional arguments but 1 was given
1 answer
I'm trying to use eval()
to run dynamic code, but it seems to reject object-based methods:
def GetHelp():
return 'Help'
class MyClass:
def __init__(self):
self.child = 5
def GetChild():
return self.child
x = eval('Get{}()'.format('Help'))
print(x)
obj = MyClass()
y = eval('obj.Get{}()'.format('Child'))
print(y)
The above code gives me:
Help
Traceback (most recent call last):
File "/Users/kakyo/Desktop/0.Dev/playground/python/hello_eval.py", line 14, in <module>
y = eval('obj.Get{}()'.format('Child'))
File "<string>", line 1, in <module>
TypeError: GetChild() takes 0 positional arguments but 1 was given
I tried to add obj to locals
:
y = eval('obj.Get{}()'.format('Child'), {'obj': obj})
but got the same error.
python built-in
marked as duplicate by deceze♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Why is there a downvote?
– kakyo
yesterday
This has nothing to do witheval
, the problem can be reproduced with a plainobj.GetChild()
.
– deceze♦
yesterday
add a comment |
This question already has an answer here:
TypeError: generatecode() takes 0 positional arguments but 1 was given
1 answer
I'm trying to use eval()
to run dynamic code, but it seems to reject object-based methods:
def GetHelp():
return 'Help'
class MyClass:
def __init__(self):
self.child = 5
def GetChild():
return self.child
x = eval('Get{}()'.format('Help'))
print(x)
obj = MyClass()
y = eval('obj.Get{}()'.format('Child'))
print(y)
The above code gives me:
Help
Traceback (most recent call last):
File "/Users/kakyo/Desktop/0.Dev/playground/python/hello_eval.py", line 14, in <module>
y = eval('obj.Get{}()'.format('Child'))
File "<string>", line 1, in <module>
TypeError: GetChild() takes 0 positional arguments but 1 was given
I tried to add obj to locals
:
y = eval('obj.Get{}()'.format('Child'), {'obj': obj})
but got the same error.
python built-in
This question already has an answer here:
TypeError: generatecode() takes 0 positional arguments but 1 was given
1 answer
I'm trying to use eval()
to run dynamic code, but it seems to reject object-based methods:
def GetHelp():
return 'Help'
class MyClass:
def __init__(self):
self.child = 5
def GetChild():
return self.child
x = eval('Get{}()'.format('Help'))
print(x)
obj = MyClass()
y = eval('obj.Get{}()'.format('Child'))
print(y)
The above code gives me:
Help
Traceback (most recent call last):
File "/Users/kakyo/Desktop/0.Dev/playground/python/hello_eval.py", line 14, in <module>
y = eval('obj.Get{}()'.format('Child'))
File "<string>", line 1, in <module>
TypeError: GetChild() takes 0 positional arguments but 1 was given
I tried to add obj to locals
:
y = eval('obj.Get{}()'.format('Child'), {'obj': obj})
but got the same error.
This question already has an answer here:
TypeError: generatecode() takes 0 positional arguments but 1 was given
1 answer
python built-in
python built-in
edited 20 hours ago
DeepSpace
36.6k44169
36.6k44169
asked yesterday
kakyo
2,46743458
2,46743458
marked as duplicate by deceze♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by deceze♦
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Why is there a downvote?
– kakyo
yesterday
This has nothing to do witheval
, the problem can be reproduced with a plainobj.GetChild()
.
– deceze♦
yesterday
add a comment |
Why is there a downvote?
– kakyo
yesterday
This has nothing to do witheval
, the problem can be reproduced with a plainobj.GetChild()
.
– deceze♦
yesterday
Why is there a downvote?
– kakyo
yesterday
Why is there a downvote?
– kakyo
yesterday
This has nothing to do with
eval
, the problem can be reproduced with a plain obj.GetChild()
.– deceze♦
yesterday
This has nothing to do with
eval
, the problem can be reproduced with a plain obj.GetChild()
.– deceze♦
yesterday
add a comment |
1 Answer
1
active
oldest
votes
The error is unrelated to the fact you used eval
(but do not use eval
). It is because GetChild
does not accept self
(or actually any other argument) as the instance in its signature.
It should be def GetChild(self)
But (if it wasn't clear already) eval
should be avoided at all costs and is very rarely the only solution to a given problem.
In this case, it is preferable to use getattr
:
obj = MyClass()
y = getattr(obj, 'Get{}'.format('Child'))()
print(y)
My bad!!! OK, thanks a lot!
– kakyo
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The error is unrelated to the fact you used eval
(but do not use eval
). It is because GetChild
does not accept self
(or actually any other argument) as the instance in its signature.
It should be def GetChild(self)
But (if it wasn't clear already) eval
should be avoided at all costs and is very rarely the only solution to a given problem.
In this case, it is preferable to use getattr
:
obj = MyClass()
y = getattr(obj, 'Get{}'.format('Child'))()
print(y)
My bad!!! OK, thanks a lot!
– kakyo
yesterday
add a comment |
The error is unrelated to the fact you used eval
(but do not use eval
). It is because GetChild
does not accept self
(or actually any other argument) as the instance in its signature.
It should be def GetChild(self)
But (if it wasn't clear already) eval
should be avoided at all costs and is very rarely the only solution to a given problem.
In this case, it is preferable to use getattr
:
obj = MyClass()
y = getattr(obj, 'Get{}'.format('Child'))()
print(y)
My bad!!! OK, thanks a lot!
– kakyo
yesterday
add a comment |
The error is unrelated to the fact you used eval
(but do not use eval
). It is because GetChild
does not accept self
(or actually any other argument) as the instance in its signature.
It should be def GetChild(self)
But (if it wasn't clear already) eval
should be avoided at all costs and is very rarely the only solution to a given problem.
In this case, it is preferable to use getattr
:
obj = MyClass()
y = getattr(obj, 'Get{}'.format('Child'))()
print(y)
The error is unrelated to the fact you used eval
(but do not use eval
). It is because GetChild
does not accept self
(or actually any other argument) as the instance in its signature.
It should be def GetChild(self)
But (if it wasn't clear already) eval
should be avoided at all costs and is very rarely the only solution to a given problem.
In this case, it is preferable to use getattr
:
obj = MyClass()
y = getattr(obj, 'Get{}'.format('Child'))()
print(y)
edited 20 hours ago
answered yesterday
DeepSpace
36.6k44169
36.6k44169
My bad!!! OK, thanks a lot!
– kakyo
yesterday
add a comment |
My bad!!! OK, thanks a lot!
– kakyo
yesterday
My bad!!! OK, thanks a lot!
– kakyo
yesterday
My bad!!! OK, thanks a lot!
– kakyo
yesterday
add a comment |
Why is there a downvote?
– kakyo
yesterday
This has nothing to do with
eval
, the problem can be reproduced with a plainobj.GetChild()
.– deceze♦
yesterday