0

I am a newbie developer. I have used the code below but I would like to understand how the last line works, can someone please explain to me the last line return eval(((str(a)+"*")*b)[:-1]) of the code?

def power(a,b):
    if b == 0:
        return 1
    else:
        return eval(((str(a)+"*")*b)[:-1])
Indent
  • 4,675
  • 1
  • 19
  • 35
Theodore
  • 55
  • 4
  • 1
    This is really terrible code. If you want to compute powers, just use the `**` operator. If you want to use the task of computing powers as an opportunity to learn about computer programming basics, use a loop or recursion, not `eval`. – user2357112 Dec 24 '18 at 07:41
  • Read about [Python's `eval`](https://docs.python.org/3/library/functions.html?highlight=eval#eval) builtin. In general, refer to [Python's documentation](https://docs.python.org/3/). Indeed, that code is really bad. BTW, an excellent introduction to programming is [SICP](https://mitpress.mit.edu/sicp/) but does not use Python – Basile Starynkevitch Dec 24 '18 at 07:51
  • Nice question, too ... and even when closed out, you could accept an answer here, too. Which would actually push you to "upvote rights", which gives in more ways to appreciate, like when there are multiple helpful answers on a question ;-) – GhostCat Jan 16 '19 at 09:00
  • I appreciate the assistance – Theodore Jan 16 '19 at 10:49

5 Answers5

0

return eval(((str(a)+"*")*b)[:-1])

is equivalent to

a_str=str(a)               # convert a in STRING
a_star=a_str+"*"           # concat the string a_str with "*"
a_repeted=a_star*b         # repeat a_star "b" times
exp=(a_repeted)[:-1]       # get all a_repeted car except the last on (ex 3*3*3*3 for a=3 and b=4)
res=eval(exp)              # evalutate the expression
return res

it'is equivalent to (really better ;-) !) :

def power(a,b):
    return a ** b
Indent
  • 4,675
  • 1
  • 19
  • 35
0

The best way is to use a**b for computing power. However, if you want to use eval then consider joining the string in this way: eval('*'.join(str(a)*b)). In this, str(a) will convert integer a to string a then *b will repeat the a b times and finally the '*'.join() will join all the a's together with * in between and create a string something like a*a*a*a. eval will then evaluate the string as if it is an expression.

Rish
  • 804
  • 8
  • 15
0

eval evaluates python code or lets Python program run Python code within itself. example:

CODE:

a = 15 eval('print(a + 3)')

OUTPUT:
18

when you return the following

eval(((str(a)+"*")*b)[:-1])

what you basically doing is this( for example if you are computing power(2,5)):

str(a) -> changes the value of a to string. in this case "2"
str(a)+"*" -> concatinate the above string to the multiplication sign. now we have "2*"
(str(a)+"*")*b) -> duplicates the above string b times. That is "2*"+"2*"+"2*"+"2*"+"2*", that is five times and now you have "2*2*2*2*2*"

But as you can see there is an extra "*" at the end. To remove this one we use [:-1]. what this basically doing is select all except the last one. ":" basically means all.

so the final expression to be evaluated is "2*2*2*2*2". which is 2^5.

PyHomer
  • 79
  • 6
-1

eval() is never a good idea to use. Read about its dangerous behaviour here.

You just need ** operator which is equivalent to ^ (raise to) operator you see in other languages:

def power(a,b):
    return a ** b
Austin
  • 25,759
  • 4
  • 25
  • 48
-1

A terrible idea, as others have said - if you're a newbie, consider finding a better guide!

eval reads a string and evaluates it as if it were not (as if it is not in quotes). So we construct a string of a ** b (ie a^b), and ask Python to read it, then return all but the last character. Pretty pointless but there you go.

Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75