Exemple #1
0
import b

b.spam("hello")

Exemple #2
0
import b
b.spam('gumby')
Exemple #3
0
#!/usr/bin/env python
import b
b.spam('sample')
Exemple #4
0
import b
import sys

b.spam('alabala')

print(b.text_to_list('alabala'))

# print(sys.path)
# print(sys.modules.keys())
Exemple #5
0
import b
import sys

b.spam('hello')

print(sys.path)
"""
In short modules provide as easy way to organize components into a system by serving as a self contained package of
variables known as namespaces.
Ultimately, Python's modules allow us to link individual files into a larger program system.
* Code reuse
* System namepace partitioning
"""

# Let's try to understand concept of imports and attributes with an example

# Let's say this is our top level script
# I am going to create a script/module b.py which we will import here
# look into the code of b.py to see what it has

import b
b.spam("imported")  # Outputs : imported spam

# We can also can chain/ hierarchy of the modules to use import into one another
# Python itself has more than 200 modules, called the standard library, which can be used on any platform where you are
# trying to run python.

# HOW IMPORT WORKS
"""
* Much similar to the C programmers like to compare the Python module import operation to a C #include, but they 
really shouldn't . In Python, imports are not just textual insertions of one file into another. They are really runtime
operations that perform three distinct steps the first time a program imports a given file.

* find module's file 
* compile to bytecode
* Run the module's code
Note : 
Exemple #7
0
import sys
import b

print(sys.path)

b.spam('from a.py')
Exemple #8
0
import b  # a.py 파일
b.spam('gumby')  # 'gumby spam'을 출력
Exemple #9
0
# Любой файл может импортировать функциональные возможности из любого
# другого файла.
import b

b.spam('Hello')

# можно импортировать только нужные вам имена
from b import foo, bar

print(foo, bar)
foo = 4

# можно импортировать все имена модуля в область видимости
from b import *

spam('Hey Hey Hey')

# двойное импортирование
# a может использовать имена из b и с. однако обратное не верно:
# c не имеет доступа к b, а b к а.
b.c.printing('c?')

# доступ к именам модуля
print(b.__dict__.keys())
print(dir(b))

# from string import ascii_letters # ImportError: cannot import name ascii_letters
# при наличии файла string.py в папке с проектом он будет воспринят как
# модуль, и при импорте будет импортирован именно он

import sys
Exemple #10
0
import b
b.spam('banana')

import sys
print(sys.path)

from b import spam
spam('more bananas')

from module1 import *
printer("Hello world")

from small import x, y
x = 42
y[0] = 42
print(x, y)

#This chapter is a bit heavy on the theory.
#I am unlikely to require knowledge beyond the basics in this area for the simple programs I write
Exemple #11
0
x = 1
y = 2
import b

print x,
print b.x,
print b.c.x

b.x = 6

b.spam('call from a')
a_var = 1
Exemple #12
0
### file: b.py

def spam(text):
    print(text, 'spam')



### file: a.py

import b
b.spam('gumby')


# NOTE: described but not shown explicitly in the book:
C:\misc> a.py
gumby spam



### fileL: C:\Python30\pydirs.pth
c:\pycode\utilities
d:\pycode\package1



>>> import sys
>>> sys.path
['', 'C:\\users', 'C:\\Windows\\system32\\python30.zip', 'c:\\Python30\\DLLs', 
'c:\\Python30\\lib', 'c:\\Python30\\lib\\plat-win', 'c:\\Python30', 
'C:\\Users\\Mark', 'c:\\Python30\\lib\\site-packages']
Exemple #13
0
x = 1
y = 2
import b 

print x,
print b.x,
print b.c.x

b.x = 6

b.spam('call from a')
a_var = 1
Exemple #14
0
#coding=utf-8
'''
Created on 2018年9月26日

@author: Pan
'''
from functools import reduce

print(reduce(lambda x, y: x + y, (1, 2, 3, 4, 5)))
print(reduce(lambda x, y: x * y, (1, 2, 3, 4, 5)))


def myreduce(function, seq):
    first = seq[0]
    for next in seq[1:]:
        first = function(first, next)
    return first


print(myreduce(lambda x, y: x + y, (1, 2, 3, 4, 5)))
print(myreduce(lambda x, y: x * y, (1, 2, 3, 4, 5)))

import operator, functools

x = functools.reduce(operator.add, (2, 3, 4))
print(x)

from b import spam

spam("a")