Skip to content

Instantly share code, notes, and snippets.

@kaito834
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaito834/71cb148035e16aa0f20b to your computer and use it in GitHub Desktop.
Save kaito834/71cb148035e16aa0f20b to your computer and use it in GitHub Desktop.
This is snippet to import my module; myTestModule.py and import-myTestModule.py were saved on same directory.
#!/usr/bin/env python
# coding: utf-8
# tested by Python 3.4.3 on Windows 8.1
# Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
'''
If you does NOT create .pyc file, run python.exe with -B option or
set non-empty value on PYTHONDONTWRITEBYTECODE, environment variable.
Ref.
https://docs.python.org/3/using/cmdline.html#cmdoption-B
https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE
'''
import myTestModule
def main():
A1 = myTestModule.A()
print("{0}: myTestModule.A.str1 on main()".format(myTestModule.A.str1))
print("{0}: A1.str1 on main()".format(A1.str1))
print("{0}: A1.str2 on main()".format(A1.str2))
A1.a()
print('')
# https://docs.python.org/3/library/functions.html#vars
# p.124 6.3.4 名前空間の参照, ”Python文法詳解", O'REILLY
print("vars(myTestModule):")
for key in vars(myTestModule): print(' ', key)
print("vars(myTestModule.A):")
for key in vars(myTestModule.A): print(' ', key)
print("vars(A1):")
for key in vars(A1): print(' ', key)
A1.b()
if __name__ == '__main__':
main()
#!/usr/bin/env python
# coding: utf-8
# tested by Python 3.4.3 on Windows 8.1
# Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
class A():
str1 = 'test1'
__str3 = 'test3'
list1 = [
'list1-1',
'list1-2',
'list1-3'
]
def __init__(self):
self.str2 = 'test2'
def a(self):
print("{0}: A.str1 on a()".format(A.str1))
print("{0}: str2 on a()".format(self.str2))
def b(self):
print("vars(self) on b():")
for key in vars(self): print(' ', key)
$>c:\Python34\python.exe -B import-myTestModule.py
test1: myTestModule.A.str1 on main()
test1: A1.str1 on main()
test2: A1.str2 on main()
test1: A.str1 on a()
test2: str2 on a()
vars(myTestModule):
__builtins__
__file__
__name__
__doc__
__package__
__spec__
A
__cached__
__loader__
vars(myTestModule.A):
_A__str3
__doc__
a
str1
list1
__weakref__
__dict__
b
__module__
__init__
vars(A1):
str2
vars(self) on b():
str2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment