Exemplo n.º 1
0
#otherfile.py

import manynames

X = 66
print(X)
print(manynames.X)

manynames.f()
manynames.g()

print (manynames.C.X)
I = manynames.C()
print(I.X)
I.m()
print(I.X)
Exemplo n.º 2
0
## 老鼠书_第29章
## otherfile.py

import manynames

X = 66
print(X)
print(manynames.X)

manynames.f()
manynames.g()

print(manynames.C.X)
I = manynames.C()
print(I.X)

I.m()
print(I.X)
Exemplo n.º 3
0
# -*- coding: utf-8 -*-
# otherfile.py

import manynames

X = 66
print(X)  # 66: 여기까지는 지역 변수
print(manynames.X)  # 11: 임포트 이후 X는 속성이 됨

manynames.f()  # 11: manynames의 X
manynames.g()  # 22: 다른 파일의 함수에 있는 지역 변수
print(manynames.C.X)  # 33: 다른 모듈 내의 클래스 속성임
I = manynames.C()
print(I.X)  # 33: 여기까지는 아직 클래스의 속성임
I.m()
print(I.X)  # 55: 이제 인스턴스의 속성임
Exemplo n.º 4
0
#!/usr/bin/env python
# coding: utf-8
import manynames

X = 66
print X
print manynames.X
print manynames.f()
print manynames.g()

print manynames.C.X
I = manynames.C()
print I.X
I.m()
print I.X
Exemplo n.º 5
0
#-----------------------------------------------
# Usage: otherfile.py
# Description: namespace and scope
#-----------------------------------------------



import manynames


x = 66
print(x)                                            # 66: the global here
print(manynames.x)                                  # 11: globals become attributes after imports


manynames.f()                                       # 11: manynames' x, not the one here
manynames.g()                                       # 22: local in other file's function

print(manynames.C.x)                                # 33: attribute of class in other module

I = manynames.C()
print(I.x)                                          # 33: still from class here

I.m()
print(I.x)                                          # 55: now from instance


'''
Notice here how manynames.f() prints the X in manynames, not the X assigned in this file
—scopes are always determined by the position of assignments in your source code
(i.e., lexically) and are never influenced by what imports what or who imports whom.
Exemplo n.º 6
0
#!/usr/bin/env python

# otherfile.py

import manynames

x = 66
print(x)  # 66: the global here
print(manynames.x)  # 11: global become attributes after imports

manynames.f()  # 11: manynames's x, not the one here!
manynames.g()  # 22: local in other file's function

print(manynames.C.x)  # 33: attribute of class in other module
I = manynames.C()
print(I.x)  # 33: still from class here
I.m()
print(I.x)  # 55: now from instance!
Exemplo n.º 7
0
# otherfile.py

import manynames

X = 66
print(X)                # 66: the global here
print(manynames.X)      # 11: globals become attributes after imports

manynames.f()           # 11: manynames's X, not the one here!
manynames.g()           # 22: local in other file's function

print(manynames.C.X)    # 33: attribute of class in other module
I = manynames.C()
print(I.X)              # 33: still from class here

I.m()
print(I.X)              # 55: now from instance!

Exemplo n.º 8
0
# coding=utf-8

import manynames

X = 66

print X  # 66: the global here

print manynames.X  # 11: globals become attributes after imports

manynames.f()  # 11: func f() print manyname.X
manynames.g()  # 22: local in other file's function

print manynames.C.X  # 33: attribute of class in other module

I = manynames.C()
print I.X  # 33: still from class here

I.m()
print I.X  # 55: now from instance
Exemplo n.º 9
0
import manynames

X = 66
print(X)
print(manynames.X)  # атрибут модуля
manynames.f()  # 11
manynames.g()

print(manynames.C.X)
I = manynames.C()
print(I.X)
I.m()
print(I.X)
Exemplo n.º 10
0
#!/usr/bin/env python3

import manynames

X = 66
print(X) 						# 66: здешняя глобальная переменная
print(manynames.X) 	# 11: глобальная, ставшая атрибутом в результате импорта

manynames.f() 			# 11: X в manynames, не здешняя глобальная!
manynames.g() 			# 22: локальная в функции, в другом файле

print(manynames.C.X)# 33: атрибут класса в другом модуле
I = manynames.C()
print(I.X) 					# 33: все еще атрибут класса
I.m()
print(I.X) 					# 55: а теперь атрибут экземпляра!

Exemplo n.º 11
0
import manynames

X = 66
print(X) #66 global
print(manynames.X) #11, global becomes attribute after import

manynames.f() #11
manynames.g() #22

print(manynames.C.X) #33
I = manynames.C()
print(I.X) #33 still from class
I.m()
print(I.X) #55




#!/usr/bin/env python3
"""
manyname_other.py: access variables within manynames from another file
and declares some variables within its own scope (as a learning experience)
"""

import manynames

X = 66
print(X)                # 66: the global here
print(manynames.X)      # 11: globals become attributes after imports

manynames.f()           # 11: manyname's X, not the one here!
manynames.g()           # 22: local in other file's function

print(manynames.C.X)    # 33: attribute of class in other module
I = manynames.C()
print(I.X)              # 33: still from class here
I.m()
print(I.X)              # 55: now from instance!


# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
Exemplo n.º 13
0
import manynames

x = 66
print(x)  # 66
print(manynames.x)  # 11

manynames.f()  # 11,打印的是manynames的x,而不是本文件的x,作用域总是由源代码的赋值语句位置来决定的。
manynames.g()  # 22

print(manynames.C.x)  # 33
obj = manynames.C()
print(obj.x)  # 33
obj.m()
print(obj.x)  # 55