class Something(object): pass # we need this because setting attributes on object instances is not allowed, e.g. object().x = 3 is not supported def non_operation(a): # gets a reference to anything (a primary data type or an object) a = 3 # substitues a in this scope with 3 number, name, truthy, another_number, some_object, a_dict = 1, 'test', True, 1.1, Something(), {1:2} print(number, name, truthy, another_number, some_object, a_dict) non_operation(number) non_operation(name) non_operation(truthy) non_operation(another_number) non_operation(some_object) non_operation(a_dict) print('nothing has changed:') print(number, name, truthy, another_number, some_object, a_dict) def cache_data(a, to_cache_number): # gets a 2 references b = a # creates a reference 'b' to the object also referenced by 'a' b.some_attribute = to_cache_number # substitues 'some_attribute' attribute on the object referenced by 'b' with the value 'to_cache_number' a = 3 # the reference 'a' not contains the primary data type int, value 3 a += 10 # the reference 'a' not contains the primary data type int, value 13 c = b c.some_attribute += 1 return b # returns the address of the value referenced by 'b' cache_data(some_object, 3) print('Will be the initial object ', some_object, 'since nobody can take that away from us and 4 since the value of the attribute was changed', some_object.some_attribute) def sum(first_number=1, second_number=1): return first_number + second_number print('sums') print(sum(1, 10)) first_number, second_number = 1, 10 print(sum(first_number, second_number)) # not needed, there is no relation # but what is nicer is to be clear in what your are doing # assuming sum was defined in another module print(sum(first_number=1, second_number=10)) # is a lot clear to user than sum(1, 10)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question