-2

enter image description here

anyone can explain why when we declare as list, it will cross different method, but if declare as int, it can only use inside of method.

Thanks

SCB
  • 5,821
  • 1
  • 34
  • 43
vin.g
  • 1
  • 2
    Please don't screenshot your code. Add it as raw text and then format using the `{}` code format button. It makes it easier for people to read and test and means that search engines can index it. – SCB Mar 06 '18 at 04:11
  • Possible duplicate of [Passing an integer by reference in Python](https://stackoverflow.com/questions/15148496/passing-an-integer-by-reference-in-python) – SCB Mar 06 '18 at 04:16
  • Please provide your code not an image. Read through (How to create a https://stackoverflow.com/help/mcve – Amit Phaltankar Mar 06 '18 at 04:47

1 Answers1

0

In the example where you have l and n as ints, you aren't actually modifying l or n before you print them. You are incrementing p and k but then since p and k aren't assigned to anything you are throwing the computation away.

Additionally, ints in Python are immutable. So, if you wanted to store an updated value, your class should have properties that you update accordingly such as self.l and self.n.

Kent Sommer
  • 358
  • 3
  • 7