mirror of
https://github.com/erg-lang/erg.git
synced 2025-10-02 13:41:10 +00:00
translate save
This commit is contained in:
parent
3b5bbb59bc
commit
d0bb616c73
10 changed files with 168 additions and 168 deletions
|
@ -1,9 +1,9 @@
|
|||
# Integration with Python
|
||||
# 与 Python 集成
|
||||
|
||||
## Export to Python
|
||||
## 导出到 Python
|
||||
|
||||
When the Erg script is compiled, a .pyc file is generated, which can simply be imported as a Python module.
|
||||
However, variables set to private on the Erg side cannot be accessed from Python.
|
||||
编译 Erg 脚本时,会生成一个 .pyc 文件,可以简单地将其作为 Python 模块导入。
|
||||
但是,无法从 Python 访问在 Erg 端设置为私有的变量。
|
||||
|
||||
```python
|
||||
# foo.er
|
||||
|
@ -19,26 +19,26 @@ erg --compile foo.er
|
|||
import foo
|
||||
|
||||
print(foo.public)
|
||||
print(foo.private) # AttributeError:
|
||||
print(foo.private) # 属性错误:
|
||||
```
|
||||
|
||||
## Import from Python
|
||||
## 从 Python 导入
|
||||
|
||||
All objects imported from Python are by default of type `Object`. Since no comparisons can be made at this point, it is necessary to refine the type.
|
||||
默认情况下,从 Python 导入的所有对象都是“Object”类型。 由于此时无法进行比较,因此有必要细化类型。
|
||||
|
||||
## Type Specification in the Standard Library
|
||||
## 标准库中的类型规范
|
||||
|
||||
All APIs in the Python standard library are type specified by the Erg development team.
|
||||
Python 标准库中的所有 API 都是由 Erg 开发团队指定的类型。
|
||||
|
||||
```python
|
||||
time = pyimport "time"
|
||||
time.sleep! 1
|
||||
```
|
||||
|
||||
## Type Specification for User Scripts
|
||||
## 用户脚本的类型规范
|
||||
|
||||
Create a `foo.d.er` file that types the Python `foo` module.
|
||||
Type hints on the Python side are ignored since they are not 100% guaranteed.
|
||||
创建一个类型为 Python `foo` 模块的 `foo.d.er` 文件。
|
||||
Python 端的类型提示被忽略,因为它们不是 100% 保证的。
|
||||
|
||||
```python
|
||||
# foo.py
|
||||
|
@ -63,11 +63,12 @@ foo = pyimport "foo"
|
|||
assert foo.bar(1) in Int
|
||||
```
|
||||
|
||||
This ensures type safety by performing type checking at runtime. The ``declare`` function works roughly as follows.
|
||||
这通过在运行时执行类型检查来确保类型安全。 ``declare`` 函数大致如下工作
|
||||
|
||||
|
||||
```python
|
||||
declare|S: Subroutine| sub!: S, T =
|
||||
# Actually, => can be cast to a function without block side effects
|
||||
# 实际上,=> 可以强制转换为没有块副作用的函数
|
||||
x =>
|
||||
assert x in T.Input
|
||||
y = sub!(x)
|
||||
|
@ -75,7 +76,7 @@ declare|S: Subroutine| sub!: S, T =
|
|||
y
|
||||
```
|
||||
|
||||
Since this is a runtime overhead, a project is planned to statically type analyze Python scripts with Erg's type system.
|
||||
由于这是运行时开销,因此计划使用 Erg 的类型系统对 Python 脚本进行静态类型分析
|
||||
|
||||
<p align='center'>
|
||||
<a href='./31_pipeline.md'>上一页</a> | <a href='./33_package_system.md'>下一页</a>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue