我有以下文件夹结构:
/main
main.py
/io
__init__.py
foo.py
Run Code Online (Sandbox Code Playgroud)
在Python 2.7中,我将在以下内容中编写以下内容main.py:
import io.foo
Run Code Online (Sandbox Code Playgroud)
要么
from io.foo import *
Run Code Online (Sandbox Code Playgroud)
Python 3.5中的wheareas我收到导入错误:
Traceback (most recent call last):
File "./main.py", line 6, in <module>
import io.foo
ImportError: No module named 'io.foo'; 'io' is not a package
Run Code Online (Sandbox Code Playgroud)
到目前为止我找不到任何帮助.
我目前正在实现一个可以处理与物理单位相关的数字数据的类。
我想实现一种计算实例平方根的方法。假设您有一个具有属性值和名称的类实例:
from math import sqrt
class Foo:
def __init__(self, value, name)
self.value = value
self.name = name
def __sqrt__(self):
return sqrt(self.value)
Run Code Online (Sandbox Code Playgroud)
我想实现一个类似于add (self, other) 等魔术方法的函数,当我调用 math.sqrt() 函数时,它会计算平方根:
A = Foo(4, "meter")
root = math.sqrt(A)
Run Code Online (Sandbox Code Playgroud)
应该返回调用A.sqrt ( )函数。
我想在Python中检查(大整数)值是否为10的幂。通常,我只会检查该值的模10。这对于不超过1.E + 22的值都可以正常工作,然后返回奇怪的值:
lowInteger = 1.E12
highInteger = 1.E23
print(lowInteger % 10) #as expected returns 0
>>> 0.0
print(highInteger % 10) #returns 2.0 instead of 0.0, 1.E23 returns 3.0, etc
>>> 2.0
Run Code Online (Sandbox Code Playgroud)