Ejemplo n.º 1
0
    import module
    from module import func
    from module import func1, func2, ...
    from module import * # 导入所有函数

    # 使用不同模块的同名函数
    module1.open()
    module2.open()

    # 为模块起别名
    import math as foobar
    print foobar.sqrt(9.0)

    # 为函数起别名
    from math import sqrt as foobar
    print foobar(9.0)

赋值 
    序列解包
    x, y, z = 1, 2, 3
    print x, y, z # 1 2 3

    x, y = y, x # swap x, y

    values = 1, 2, 3
    print values # (1, 2, 3)

    x, y, z = values
    print x # 1                         # FIXME: values的len必须和左边的变量数量一致

    # 链式赋值
Ejemplo n.º 2
0
# import somemodule
# from somemodule import somefunction
# from somemodule import somefunction, anotherfunction...
# from somemodule import *
import math as foobar

print foobar.sqrt(4)

from math import sqrt as foobar

print foobar(4)

# sequence unpacking
x, y, z = 1, 2, 3
print x, y, z  # 1, 2, 3
x, y = y, x
print x, y, z  # 2, 1, 3

values = 1, 2, 3
print values
x, y, z = values
print x, y, z  # 1, 2, 3

scoundrel = {"name": "Robin", "girlfriend": "Marion"}
key, value = scoundrel.popitem()
print key, value

# following both will throw exception
# x, y, z = 1, 2
# x, y, z = 1, 2, 3, 4
Ejemplo n.º 3
0
import math
from cmath import sqrt

x = math.floor(21.9)
print x

y = int(math.floor(21.9))
print y

z = sqrt(-1)
print z

w = (1 + 3j) * (2 + 4J)
print w

# import somemoudle

# from somemoudle import somefunction

# from somemoudle import somefunction anothersomefunction yetanothersomefunction

# from somemoudle import *

import math as foobar

foobar.sqrt(4)

from math import sqrt as foobar
foobar(4)
Ejemplo n.º 4
0
import math as foobar
print foobar.sqrt(4)

from math import sqrt as foobar
print foobar(4)

# nice formate of python
x, y, z = 3, 4, 10
print y, z, x
x, y = y, x
print x, y, z
# more explicit
print "more interesting "
values = 1, 2, 3
print values
x, y, z = values
print y
scundrel = {'name': 'Emon', 'boyFriend': 'sobuj'}
key, value = scundrel.popitem()
print 'popitem using by ', key
print 'value is ', value
Ejemplo n.º 5
0
print('Age: ', 24)
print(1, 2, 3)
print((1, 2, 3))
name = 'Gumby'
salutation = 'Mr.'
greeting = 'Hello,'
print(greeting, salutation, name)
greeting = 'Hello'
print(greeting, ',', salutation, name)
print(greeting + ',', salutation, name)
print('Hello'),
print('world!')
import math as foobar
foobar.sqrt(4)
from math import sqrt as foobar
foobar(4)

# 解包
x, y, z = 1, 2, 3
print(x, y, z)
x, y = y, z
print(x, y, z)
scoundrel = {'name': 'Robin', 'girlfriend': 'Marion'}
key, value = scoundrel.popitem()
print(key),
print(value)
# 解包 新
a, b, *rest = [1, 2, 3, 4]

x = y = people.get('mlh')
#等价于
Ejemplo n.º 6
0
'''evil with the import statement'''

import sys


def foo_func(x):
    return x * x


sys.modules['foobar'] = foo_func
import foobar

print foobar(10)


class Klass(object):
    '''a classy class'''
    def bar(self, x):
        return x * x * x


sys.modules['classy'] = Klass
import classy

print classy().bar(10)

sys.modules['yarr'] = Klass()
import yarr

print yarr.bar(10)
Ejemplo n.º 7
0
from somemodule import *

#如果两个模块都有open函数,就需要像下面这样使用函数

module1.open(...)
module2.open(...)

# 或,在语句末尾增加一个as子句,在该as子句后给出的名字,将
# 成为整个模块的别名
import math as foobar
foobar.sqrt(4)

# 同样可以为函数提供别名
from math import sqrt as foobar
foobar(9)

#序列解包(sequence unpacking),可选代解包
x,y,z = 1,2,3
print x, y, z

x,y = y,x
print x, y

scoundrel = {'name':'robin', 'girlfriend':'marion'}
key, value = scoundrel.popitem()
print key, value

# 需要注意的是,所解包的序列中的元素数量必须和放置在赋值符号=左边的变量数量
# 完全一致,否则python会赋值时引发异常
Ejemplo n.º 8
0
或者
from somemodule import somefunction
或者
from somemodule import somefunction,anotherfunction,yetanotherfunction
或者
from somemodule import *
只有确定自己想要从给定的模块导入所有功能时,才应该使用最后一个版本。但是如果两个模块都有open函数,那又该怎么办?只需使用第一种方式导入,然后像下面这样使用函数:
module1.open(...)
module2.open(...)
但还有另外的选择:可以在语句末尾增加一个as子句,在该子句后给出名字,或为整个模块提供别名:
>>> import math as foobar
>>> foobar.sqrt(4)
2.0
也可以为函数提供别名:
>>> from math import sqrt as foobar
>>> foobar(4)
2.0
对于open函数,可以像下面这样使用:
from module1 import open as open1
from module2 import open as open2
注意:有些模块,例如os.path是分层次安排的(一个模块在另一个模块的内部).
就算是不起眼的赋值语句也有一些特殊的技巧
序列解包
赋值语句的例子已经给过不少,其中包括对变量和数据结构成员的(比如列表中的位置和分片以及字典中的槽)赋值。但赋值的方法还不止这些。比如,多个赋值操作可以同时进行:
>>> x,y,z=1,2,3
>>> print x,y,z
1 2 3
>>> x,y=y,x
>>> print x,y,z
事实上,这里所做的事情叫做序列解包或可选代解包-将多个值的序列解开,然后放到变量的序列中。更形象一点的表示出来就是:
>>> values=1,2,3
Ejemplo n.º 9
0
'''evil with the import statement'''


import sys

def foo_func(x):
    return x * x


sys.modules['foobar'] = foo_func
import foobar
print foobar(10)


class Klass(object):
    '''a classy class'''
    def bar(self, x):
        return x * x * x


sys.modules['classy'] = Klass
import classy
print classy().bar(10)


sys.modules['yarr'] = Klass()
import yarr
print yarr.bar(10)


sys.modules['argh'] = Klass().bar