In [3]:
if 1:
    print("참")
else:
    print("거짓")



In [6]:
print(1 and "t" or "f")


t

In [7]:
print(1 and 0)


0

In [8]:
print(0 and 1)


0

In [9]:
print("a" and "b")


b

In [10]:
print("a" or "b")


a

In [11]:
print("a" and 0)


0

In [12]:
print(0 and 1 or 2)


2

In [13]:
print(1 and 0 or 2)


2

In [14]:
print(1 and 1 or 0)


1

In [18]:
print(1 and (lambda x: x.upper())("a") or (lambda x:x.lower())("a"))


A

In [19]:
print(1 or 1)


1

In [20]:
print(1 or 9)


1

In [21]:
print(9 or 1)


9

In [23]:
print(True or False)


True

In [25]:
print((lambda x: lambda y: y+1)(2))


<function <lambda>.<locals>.<lambda> at 0x1041c6598>

In [27]:
print((lambda x : (lambda y: y+2)(x))(3))


5

In [36]:
def test(x):
    return (lambda x : -x)(x)

In [38]:
print(test((lambda x : x+2)(3)))


-5

In [39]:
n = 3

def func():
    if n:
        total = n
    print(total)

In [40]:
func()


3

In [41]:
n = 0
func


Out[41]:
<function __main__.func>

In [47]:
var = 0 
def outter():
    var = 77
    def inner():
        nonlocal var
        var += 1
        print(var)
    return inner

In [49]:
clsr = outter()

In [50]:
clsr()


78

In [51]:
clsr()


79

In [52]:
clsr()


80

In [62]:
def a():
    temp=0
    def process():
        nonlocal temp
        temp+=1
        print(temp)
    return process

In [63]:
aa = a()
bb = a()

In [64]:
aa()


1

In [65]:
bb()
bb()
bb()


1
2
3

In [66]:
aa()


2

In [69]:
aa()


4

In [68]:
aa()


3

In [70]:
bb()


4

In [71]:
import datetime

In [74]:
def print_hi():
    print("hello python")

In [75]:
print_hi()


hello python

In [76]:
print(datetime.date)


<class 'datetime.date'>

In [79]:
print(datetime.date.today())


2017-01-04

In [87]:
print(datetime.date.today())


2017-01-04

In [88]:
def deco1(func):
    def new_func():
        print("Today ",datetime.date.today())
        func()
    return new_func

In [89]:
print_hi1 = deco1(print_hi)

In [90]:
print_hi1()


Today  2017-01-04
hello python

In [91]:
def myDeco(func):
    total = 0
    def new_func():
        nonlocal total
        total+=1
        print(total,"번 호출")
        func()
    return new_func

In [92]:
def print_hi():
    print("Hello python")

In [93]:
func1 = myDeco(print_hi)
func2 = myDeco(print_hi)

In [94]:
func1()


1 번 호출
Hello python

In [95]:
func1()


2 번 호출
Hello python

In [96]:
func2()


1 번 호출
Hello python

In [97]:
@myDeco
def print_h():
    print("hhh")

In [98]:
print_h()


1 번 호출
hhh

In [102]:
def 장식자(func):
    def temp():
        print("장식자 입니다.")
        func()
    return temp

In [103]:
@장식자
def print_hohoho():
    print("hohoho~~~")

In [104]:
print_hohoho()


장식자 입니다.
hohoho~~~

In [119]:
def func_param_test(func):
    func(name)

In [120]:
def fun(num):
    print("test", num)

In [121]:
func_param_test(fun(2))


test 2
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-121-50419032128b> in <module>()
----> 1 func_param_test(fun(2))

<ipython-input-119-5897c2cc7e20> in func_param_test(func)
      1 def func_param_test(func):
----> 2     func(name)
      3 

NameError: name 'name' is not defined

In [ ]: