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