Пример #1
0
def apply_to_list(some_list, f):  # 调用参数-列表,函数
    return [f(x) for x in some_list]  # 对于列表中的每一个元素,进行函数f(),返回列表结果
Пример #2
0
def say_hello_then_call_f(f, *args, **kwargs):  #位置参数*args,关键字参数**kwargs
    print 'args is', args  #打印所有的位置参数
    print 'kwargs is', kwargs  #打印所有的关键字参数
    print("Hello! Now I'm going to call %s" % f)  #打印字符串并调用f(该参数代表一个函数)
    return f(*args, **kwargs)  #调用函数f,并返回位置参数*args,关键字参数**kwargs的结果
Пример #3
0
def apply_to_list(some_list, f):
	return [f(x) for x in some_list]
Пример #4
0
isinstance(b, (int, float))  # 返回True

# 查看属性方法
a = 'foo,fob'
type(a)
dir(a)
help(a.split)
a.split(',')
print(a)

# 调用模块中的变量和函数
# 同及目录下调用模块需要将该文件夹设置为Source Root,并且在python console中添加该Source Root.
# 方法一
import some_module

result = some_module.f(5)
pi = some_module.PI

# 方法二
from some_module import f, g, PI

result1 = g(5, PI) + f(2)

# 方法三  别名的使用
import some_module as sm
from some_module import PI as pi, g as gf

r1 = sm.f(5)
r2 = gf(6, pi)

# 用is关键字判断两个引用是否指向同一个对象
Пример #5
0
isiterable(5) # False
isiterable([1, 2]) # True

x = "hey hi yo"

if not isinstance(x, list) and isiterable(x): # checks if not list and iterable
    x = list(x)
    print(x) # now its a list!


## imports ---------------------------------------------------------
cd /Users/dnoriega/GitHub/pyfordata/appendix

# save a file `some_module.py` for import
import some_module
result = some_module.f(5)
pi = some_module.PI
# ??some_module.g # we can look at the functions
# ??some_module.f # etc.

# equivalently
from some_module import f, g, PI
result = g(5, PI)

# by using `as` keywords, we can give imports different values
import some_module as sm
from some_module import PI as pi, g as gf

r1 = sm.f(pi) # some_module as `sm`
r2 = gf(6, pi) # PI as `pi`, g as `gf`
Пример #6
0
#sometimes, you may not care about the type of an object but rather than whether is has certain method or behavior.
def isiterable(obj):
	try:
		iter(obj)
		return True
	except TypeError:
		return False
isiterable('a string')
isiterable('[1, 2, 3]')
isiterable(5)

if not isinstance(x, list) and isiterable(x):
	x = list(x)

import some_module #some_module.py
result = some_module.f(5)
pi = some_module.PI 

from some_module import f,g,PI
result = g(5, PI)

import some_module as sm
from some_module import PI as pi, g as gf
r1 = sm.f(pi)
r2 = gf(6, pi)

#Most of the binary math operations and comparisons are as you might expect:
a is b  #True if a and b reference the same Python object
a is not b #True if a and b reference different Python objects

#strings and tuples are immutable
def g():
    return some_module.f()**2
import some_module

result = some_module.f(5)
data = some_module.PI

print "Test function call: %f, Pi is %f" % (result, data)
Пример #9
0

data = [1, 2, 3]

append_element(data, 4)

print(data)

a = 'fpp'

print(a)


def isiterable(obj):
    try:
        iter(obj)
        return True
    except TypeError:
        return False


print(isiterable(1))
print(isiterable(a))

import some_module
re = some_module.f(5)
pi = some_module.PI

print(re)
print(pi)
Пример #10
0
'''
@Author     : Md. Shamimul Islam
@Written    : 08/02/2019
@Description: python Import from module 
'''
import some_module

result = some_module.f(4)
pi = some_module.Pi

print(result)
print(pi)

#equivalently
from some_module import f, g, Pi
result01 = g(result, pi)
print(result01)

#equivalently
import some_module as sm
from some_module import Pi as p, g as gf
result02 = sm.f(9)
result03 = sm.gf(2, p)

print(result02)
print(result03)
Пример #11
0
    try:
        iter(obj)  #iter함수에 obj 대입
        return True  #obj가 iterable하다면 True 반환
    except TypeError:  # not interable 하다면
        return False  # False  반환


print(isiterable(
    [1, 2, 3]))  #isiterable 함수에 배열 [1,2,3] 대입 => 배열이므로  iterable 함 따라서 true 반환
print(isiterable(
    5))  #isiterable 함수에 변수 5 대입 => 변수 하나 이므로, not iterable 함 따라서 false 반환

# B03.The Basics. Imports
import some_module  #some_module.py를 import

result = some_module.f(5)  #result에 some_module에 선언 되어있는 함수 f에 5를 대입
print(result)  #result값 출력
pi = some_module.PI  #pi에 some_module에 선언 되어있는 변수 PI를 대입
print(pi)  #pi값 출력

from some_module import f, g, PI  #some module 모듈로 부터 f,g, PI를 불러옴
result = g(5, PI)  #result에 함수 g에 5와 PI를 대입한 결과 값을 대입
print(result)  #result 값 출력

import some_module as sm  #module some_module을 sm이라는 이름으로 선언
from some_module import PI as pi, g as gf  #some_module의 변수 PI를 pi로, 함수 g를 gf로 선언

r1 = sm.f(pi)  #바뀐 이름 sm을 가진 모듈 sm의 함수 f에 pi 값 대입
print(r1)  #r1 출력
r2 = gf(6, pi)  #함수 gf에 매개변수로 6과 pi 대입
print(r2)  #r2 출력
def say_hello_then_call_f(f, *args, **kwargs):  #가변인자, *가 한개면 입력되는 parameter를 튜플로, **가 두 개면 입력되는 parameter 사전으로
    print('args is', args)
    print('kwargs is', kwargs)
    print("Hello! Now I'm going to call %s" % f)
    return f(*args, **kwargs)

results = []
for line in file_handle:
    # keep the empty lines for now
    # if len(line) == 0:
    #   continue
    results.append(line.replace('foo', 'bar'))


# #### Function and object method calls

# In[ ]:


result = f(x, y, z)
g()


# In[ ]:


obj.some_method(x, y, z)


# In[ ]:


result = f(a, b, c, d=5, e='foo')

Пример #14
0
def isiterable(obj):
    try:
        iter(obj)
        return True
    except TypeError:
        return False


isiterable('a string')
print(isiterable('a string'))

import some_module
result = some_module.f(5)
pi = some_module.PI
print(pi)

from some_module import f, g, PI
result = g(5, PI)
print(result)

# import different variables names

import some_module as sm
from some_module import PI as pi, g as gf
r1 = sm.f(pi)
r2 = gf(6, pi)

print(r1)
print(r2)

a = [1, 2, 3]
Пример #15
0
def test1():
    assert True
    eq_(some_module.f(), 42)