mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-13 02:04:23 +02:00
Modify python to be pep8 compliant
This mostly adds spaces.
This commit is contained in:
@@ -16,7 +16,9 @@ Note: This article applies to Python 2.7 specifically, but should be applicable
|
||||
to Python 2.x. Look for another tour of Python 3 soon!
|
||||
|
||||
```python
|
||||
|
||||
# Single line comments start with a hash.
|
||||
|
||||
""" Multiline strings can be written
|
||||
using three "'s, and are often used
|
||||
as comments
|
||||
@@ -342,6 +344,7 @@ add(5, 6) #=> prints out "x is 5 and y is 6" and returns 11
|
||||
# Another way to call functions is with keyword arguments
|
||||
add(y=6, x=5) # Keyword arguments can arrive in any order.
|
||||
|
||||
|
||||
# You can define functions that take a variable number of
|
||||
# positional arguments
|
||||
def varargs(*args):
|
||||
@@ -358,6 +361,7 @@ def keyword_args(**kwargs):
|
||||
# Let's call it to see what happens
|
||||
keyword_args(big="foot", loch="ness") # => {"big": "foot", "loch": "ness"}
|
||||
|
||||
|
||||
# You can do both at once, if you like
|
||||
def all_the_args(*args, **kwargs):
|
||||
print(args)
|
||||
@@ -376,6 +380,7 @@ all_the_args(*args) # equivalent to foo(1, 2, 3, 4)
|
||||
all_the_args(**kwargs) # equivalent to foo(a=3, b=4)
|
||||
all_the_args(*args, **kwargs) # equivalent to foo(1, 2, 3, 4, a=3, b=4)
|
||||
|
||||
|
||||
# Python has first class functions
|
||||
def create_adder(x):
|
||||
def adder(y):
|
||||
@@ -400,6 +405,7 @@ filter(lambda x: x > 5, [3, 4, 5, 6, 7]) #=> [6, 7]
|
||||
## 5. Classes
|
||||
####################################################
|
||||
|
||||
|
||||
# We subclass from object to get a class.
|
||||
class Human(object):
|
||||
|
||||
@@ -486,10 +492,12 @@ def double_numbers(iterable):
|
||||
for i in iterable:
|
||||
yield i + i
|
||||
|
||||
# generator creates the value on the fly
|
||||
# instead of generating and returning all values at once it creates one in each iteration
|
||||
# this means values bigger than 15 wont be processed in double_numbers
|
||||
# note range is a generator too, creating a list 1-900000000 would take lot of time to be made
|
||||
# A generator creates values on the fly.
|
||||
# Instead of generating and returning all values at once it creates one in each
|
||||
# iteration. This means values bigger than 15 wont be processed in
|
||||
# double_numbers.
|
||||
# Note range is a generator too. Creating a list 1-900000000 would take lot of
|
||||
# time to be made
|
||||
_range = range(1, 900000000)
|
||||
# will double all numbers until a result >=30 found
|
||||
for i in double_numbers(_range):
|
||||
@@ -500,7 +508,8 @@ for i in double_numbers(_range):
|
||||
|
||||
# Decorators
|
||||
# in this example beg wraps say
|
||||
# Beg will call say. If say_please is True then it will change the returned message
|
||||
# Beg will call say. If say_please is True then it will change the returned
|
||||
# message
|
||||
from functools import wraps
|
||||
|
||||
|
||||
@@ -523,8 +532,6 @@ def say(say_please=False):
|
||||
|
||||
print(say()) # Can you buy me a beer?
|
||||
print(say(say_please=True)) # Can you buy me a beer? Please! I am poor :(
|
||||
|
||||
|
||||
```
|
||||
|
||||
## Ready For More?
|
||||
|
Reference in New Issue
Block a user