Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
3 views
in Python by (3.5k points)
edited by

There is no explanation in python documentation whether parameters are passed by value or reference and why the code used below produced an unchanged value 'Lol'

        class PassByReference:

            def __init__(self):

                self.variable = 'Lol'

                self.change(self.variable)

                print(self.variable)

    def change(self, var):

                var = 'Changed'

Is there anything else to pass the variable by actual reference?

1 Answer

0 votes
by (46k points)
edited by

You can read about definition and all other that theory in Python help, Just look at these example to make the points clear:

Using String (immutable):

Syntax:

def try_to_change_string_reference(the_string):
    print('ST', the_string)
    the_string = 'Devils in City'
    print('Will', the_string)

outer_string = 'It happened for two days'

print('before, outer_string =', outer_string)
try_to_change_string_reference(outer_string)
print('after, outer_string =', outer_string)

Output:

 before, outer_string = It happened for two days

ST It Happened for two days
Devils in the city
after, outer_string = It happened for two days

To know more about this you can have a look at the following video tutorial:-

Browse Categories

...