我有一个 foo.py
def foo():
print "test"
Run Code Online (Sandbox Code Playgroud)
在IPython中我使用:
In [6]: import foo
In [7]: foo.foo()
test
Run Code Online (Sandbox Code Playgroud)
然后我改为foo():
def foo():
print "test changed"
Run Code Online (Sandbox Code Playgroud)
在IPython中,调用的结果仍然是test:
In [10]: import foo
In [11]: foo.foo()
test
Run Code Online (Sandbox Code Playgroud)
然后我用:
In [15]: del foo
In [16]: import foo
In [17]: foo.foo()
test
Run Code Online (Sandbox Code Playgroud)
我删除了foo.pyc相同的文件夹foo.py存在,但仍然没有运气.
我可以知道如何在运行时重新导入更新的代码吗?
如何测试模块是否已在python中导入?
例如,我需要基础知识:
if not has_imported("sys"):
import sys
Run Code Online (Sandbox Code Playgroud)
也
if not has_imported("sys.path"):
from sys import path
Run Code Online (Sandbox Code Playgroud)
谢谢!
RGS.
感谢您的所有评论:代码已粘贴在此处. 自动导入文件夹中的所有子模块然后调用相同的名称函数 - python runtime inspect相关
我以这种格式定义了一个.py文件:
def foo1(): pass
def foo2(): pass
def foo3(): pass
Run Code Online (Sandbox Code Playgroud)
我从另一个文件导入它:
from foo import *
# or
import foo
Run Code Online (Sandbox Code Playgroud)
是否可以列出所有功能名称,例如["foo1", "foo2", "foo3"]?
谢谢你的帮助,我为我想要的东西上了课,如果你有建议,请评论
class GetFuncViaStr(object):
def __init__(self):
d = {}
import foo
for y in [getattr(foo, x) for x in dir(foo)]:
if callable(y):
d[y.__name__] = y
def __getattr__(self, val) :
if not val in self.d :
raise NotImplementedError
else:
return d[val]
Run Code Online (Sandbox Code Playgroud) 我可以通过下面的代码访问函数内部的python函数的属性:
def aa():
print aa.__name__
print aa.__hash__
# other simliar
Run Code Online (Sandbox Code Playgroud)
但是,如果上面的aa()函数是用于编写其他代码的模板,比方说bb(),我必须写:
def bb():
print bb.__name__
print bb.__hash__
# other simliar
Run Code Online (Sandbox Code Playgroud)
是否有类似于self类方法中的参数的"指针",所以我可以编写这样的代码?
def whatever():
print self.__name__
print self.__hash__
# other simliar
Run Code Online (Sandbox Code Playgroud)
我搜索并发现有人说使用该类来解决这个问题,但重新定义所有现有功能可能会有麻烦.有什么建议?
def attrs(**kwds):
def decorate(f):
for k in kwds:
setattr(f, k, kwds[k])
return f
return decorate
@attrs(argument_types=(int, int,), returns=int)
def add(a, b):
return a + b
Run Code Online (Sandbox Code Playgroud)
这里我需要add()可以显示其可接受的参数类型.但是我可以在运行时做这样的事吗?
ladd=[]
for x in range(0,10):
@attrs(argument_types=int, returns=int,default_parameter1 = x)
exp = lambda : add(a,x)
ladd.append(exp)
Run Code Online (Sandbox Code Playgroud)
要么
ladd=[]
for x in range(0,10):
@attrs(argument_types=int, returns=int,default_parameter1 = x)
addx = functools.partial(add, 2)
ladd.append(addx)
Run Code Online (Sandbox Code Playgroud)
我需要那些函数可以使用"decoratored"参数绑定生成运行时
好吧,这里是错误信息,我认为上面的代码无法正常工作,但我从未尝试将其粘贴到python来测试它...
>>> ladd=[]
>>> for x in range(0,10):
... @attrs(argument_types=int, returns=int,default_parameter1 = x)
... exp = lambda : add(a,x)
File "<stdin>", line 3 …Run Code Online (Sandbox Code Playgroud) 所有:
a = 1
b = a
c = b
Run Code Online (Sandbox Code Playgroud)
现在我想获得一个1标记对象的列表,这是[a, b, c].我怎么能这样做?
顺便说一句,如何在这里正式调用变量"a"?我知道到目前为止它是对象的"对象标签",但我不知道它的用语是什么.
谢谢!
为什么我需要这个:
a = b = c = 1
print a, b, c
1 1 1
a = 2
print a, b, c
2 1 1
Run Code Online (Sandbox Code Playgroud)
在其他语言中,如C,a,b,c应该是2,如果我重新指定a = 2,但在python中,没有像引用这样的东西,所以改变abc的所有值的唯一方法是a = b = c = 2据我所知,这就是为什么要获得对象的所有引用.
import itertools
def _yield_sample():
it = iter(itertools.combinations('ABCD', 2))
it2 = iter(itertools.combinations('EFGH', 3))
itc = itertools.chain(it,it2)
for x in itc:
yield x
def main():
for x in _yield_sample():
print x
Run Code Online (Sandbox Code Playgroud)
这适用于打印组合.
>>>
('A', 'B')
('A', 'C')
('A', 'D')
...
Run Code Online (Sandbox Code Playgroud)
但是这个:
def __position_combination(_count = [2,3,4,5]):
its = []
for ct in _count:
it = iter(itertools.combinations('ABCDEFG', ct))
its.append(it)
itc = itertools.chain(its)
for x in itc:
yield x
def main():
for x in __position_combination():
print x
Run Code Online (Sandbox Code Playgroud)
不会,它会打印出来
>>>
<itertools.combinations object at 0x02179210>
<itertools.combinations object …Run Code Online (Sandbox Code Playgroud) 假设图中的树状结构如下networkx:
n-----n1----n11
| |----n12
| |----n13
| |----n131
|----n2 |
| |-----n21 X
| |-----n22 |
| |----n221
|----n3
n4------n41
n5
Run Code Online (Sandbox Code Playgroud)
谢谢.
以下是一些测试itertools.tee:
li = [x for x in range(10)]
ite = iter(li)
==================================================
it = itertools.tee(ite, 5)
>>> type(ite)
<type 'listiterator'>
>>> type(it)
<type 'tuple'>
>>> type(it[0])
<type 'itertools.tee'>
>>>
>>> list(ite)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(it[0]) # here I got nothing after 'list(ite)', why?
[]
>>> list(it[1])
[]
====================play again===================
>>> ite = iter(li)
it = itertools.tee(ite, 5)
>>> list(it[1])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> …Run Code Online (Sandbox Code Playgroud) 所有,
def a(p):
return p+1
def b(func, p):
return func(p)
b(a,10) # 11
Run Code Online (Sandbox Code Playgroud)
实际上我不希望结果为"11",我想要的是一个函数对象,参数已被绑定,让我们将其命名为c.
当我使用c()或类似的东西时,它会给我结果11,可能吗?
谢谢!