def T2(x):
return x * 2
def dozen():
return T2('12')
def enestrate():
return T2(3)
print int (dozen()) + enestrate()


I know it prints 1218 but i dont understand the code and the whole concept ... I used python to run it

Trace it out.

1)

//returns 2 times the number passed in
def T2(x):
return x * 2

2)

//return T2(12) = 12*2 = 24
def dozen():
return T2('12')

3)

//returns T2(3) = 2*3 = 6
def enestrate():
return T2(3)

now this call :

print int (dozen()) + enestrate()

Split it up and analyze it. See what you can come with.

Trace it out.

//return T2(12) = 12*2 = 24
def dozen():
    return T2('12')

Split it up and analyze it.

I bet you didn't.
(Hint: T2('12') returns '1212'

I assumed some things, because I don't know python. Thanks for correcting me.

It passed in a String... T2('12').. scripting languages support operator overloading (I think that's the term)
Had it passed in an int you would've been right

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.