python: Unable to use eval() to run object methods [duplicate]












0















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.










share|improve this question















marked as duplicate by deceze python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 with eval, the problem can be reproduced with a plain obj.GetChild().
    – deceze
    yesterday
















0















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.










share|improve this question















marked as duplicate by deceze python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 with eval, the problem can be reproduced with a plain obj.GetChild().
    – deceze
    yesterday














0












0








0








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 20 hours ago









DeepSpace

36.6k44169




36.6k44169










asked yesterday









kakyo

2,46743458




2,46743458




marked as duplicate by deceze python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 with eval, the problem can be reproduced with a plain obj.GetChild().
    – deceze
    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
















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












1 Answer
1






active

oldest

votes


















3














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)





share|improve this answer























  • My bad!!! OK, thanks a lot!
    – kakyo
    yesterday


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














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)





share|improve this answer























  • My bad!!! OK, thanks a lot!
    – kakyo
    yesterday
















3














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)





share|improve this answer























  • My bad!!! OK, thanks a lot!
    – kakyo
    yesterday














3












3








3






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)





share|improve this answer














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)






share|improve this answer














share|improve this answer



share|improve this answer








edited 20 hours ago

























answered yesterday









DeepSpace

36.6k44169




36.6k44169












  • My bad!!! OK, thanks a lot!
    – kakyo
    yesterday


















  • My bad!!! OK, thanks a lot!
    – kakyo
    yesterday
















My bad!!! OK, thanks a lot!
– kakyo
yesterday




My bad!!! OK, thanks a lot!
– kakyo
yesterday



Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas