Last active
December 19, 2015 23:18
-
-
Save rendykstan/6033631 to your computer and use it in GitHub Desktop.
Python script to better understand Python Data Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
It is used to signify the absence of a value in many situations, e.g., it is returned from functions that don’t explicitly | |
return anything. | |
""" | |
print 'None - Its truth value is False.' | |
bottle = None | |
if not bottle: | |
print 'This proves that the truth value of None is False' | |
""" | |
Numeric methods and rich comparison methods may return this value if they do not implement the operation for the operands | |
provided. (The interpreter will then try the reflected operation, or some other fallback, depending on the operator.) | |
reference: http://jcalderone.livejournal.com/32837.html | |
""" | |
print 'NotImplemented - Its truth value is True.' | |
wheel = NotImplemented | |
if wheel: | |
print 'This proves that the truth value of NotImplemented is True' | |
""" | |
It is used to indicate the presence of the ... syntax in a slice. | |
""" | |
print 'Ellipsis - Its truth value is True.' | |
dust = Ellipsis | |
if dust: | |
print 'This proves that the truth value of Ellipsis is True' | |
""" | |
An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries | |
and lists are mutable. | |
""" | |
print 'List is mutable.' | |
def foo(numbers=[]): | |
numbers.append(9) | |
print numbers | |
foo() | |
foo() | |
foo() | |
print 'This proves that List is mutable' | |
print 'Number is immutable.' | |
def increment(count=0): | |
count += 1 | |
print count | |
increment() | |
increment() | |
increment() | |
print 'This proves that Number is immutable' | |
import time | |
print 'time.time() is immutable.' | |
def print_now(now=time.time()): | |
print now | |
print_now() | |
print_now() | |
print_now() | |
print 'This proves that time.time() is immutable' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment