Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python has variables!
- foo = 1
- bar = 2
- print(foo + bar)
- # and functions
- def my_sum(a, b, offset=0):
- return a + b + offset
- print(my_sum(9, 1))
- # the offset is a default argument, but you can use it
- print(my_sum(1, 2, 5))
- # funny thing? your function may return functions
- def offseted_sum_factory(offset):
- def fcn(a, b):
- return my_sum(a, b, offset)
- return fcn
- by_ten = offseted_sum_factory(10)
- by_five = offseted_sum_factory(5)
- print(by_ten(1,1))
- print(by_five(1,1))
- # take a time to think about it.... maybe you find this useless, but google for closures
- # and this functions are first class, so:
- pointer = my_sum
- print(pointer(3,2))
- # Pythons has lists
- foo = [1, 2, 3, "banana"]
- foo.append("mango")
- print(foo)
- print("Here goes the" + foo.pop())
- print(foo)
- # Python has tuples, which are immutable
- foo = (1, 2, 3, 4)
- # You may loop lists and tuples using
- for item in foo:
- print(item)
- # well.... actually it's the only for statement python knows, so if you wanna count
- for number in range(0, 100):
- print(number)
- # which replaces your good friend for(int i=0, i<100, i++)
- # You may use the tuple() and list() to convert them
- # which is nice, but useless...
- foo = [1, 2, 3]
- bar = tuple(foo)
- print(bar)
- print(list(bar))
- # Both support O(log(N)) search
- print(2 in bar)
- print(5 in foo)
- # Both support slicing using the [start:offset:pace/direction] method
- print(foo[0])
- print(foo[1])
- print(foo[1:3])
- print(foo[1::-1])
- # Sometimes you need a hash map, Python calls it a dict
- foo = {1: "one", 2: "two", "three": 3}
- # Keys must be hashable, values can be anything
- # remember: Dicts arent ordered
- # looping
- for key, value in foo.items():
- print([key, value])
- for key in foo.keys():
- print(key)
- for value in foo.values():
- print(value)
- # Access
- print(foo[2])
- print(foo['three'])
- # you may want to use the get method to avoid a KeyError if you aren't sure it exists
- value = foo.get(4)
- print(value)
- value = foo.get(5, "Bananas!")
- # is way better than using the ternary
- value = "Bananas" if foo.get(5) is None else foo[5]
- # which will go bad if foo[5] is really None
- # and don't even think about "correcting" it like:
- try:
- value = foo[5]
- except KeyError:
- value = "Bananas"
- # even if it works properly
- # well..... guess you found out that Python has exceptions
- try:
- foo = 1 + 2
- raise RuntimeError
- except RuntimeError:
- print("Foo")
- finally:
- print("Remember, the finally clause is always with you")
- # oh.... and what about that RuntimeError thing?
- # that's a subclass of Exception
- class MyException(Exception):
- pass
- # so, yeah, Python has classes.... and inheritance (multiple, diamond pattern inheritance)
- # static methods, class methods, instance methods, properties and so on....
- # it doesn't have visibility controls like private and protected. We use __ and _ prefix to indicate that.
- class Car():
- class_var = "Foobar"
- @classmethod
- def say_it(cls):
- print cls.class_var
- # Constructor
- def __init__(self):
- self._on = False
- def toggle_it(self):
- self._on = not self._on
- def drive(self, to_where):
- print("Drove to:" + to_where)
- @property
- def is_on(self):
- return self._on
- @is_on.setter
- def is_on(self, value):
- assert (value in (False, True))
- self._on = value
- # The class knows some tricks
- print(Car.class_var)
- Car.say_it()
- # your car works as expected
- ford = Car() # there's no new and the self parameter is supplied by the language
- ford.say_it()
- print(ford.is_on)
- ford.toggle_it()
- print(ford.is_on)
- ford.is_on = False
- # Let's see....
- Car.class_var = "Alice"
- Car.say_it()
- print(ford.class_var)
- # or even
- ford.say_it()
- # and if you get a fiat
- fiat = Car()
- fiat.say_it()
- # So all instances share the class variable!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement