Skip to main content

Introduction

Gython is a transpiler written in Python that converts a python like language into Go.

Syntax Documentation

Installing

Install using pip install gython.

Usage:

gython file.py

Getting Started

Gython supports classes and multiple inheritance, with method overrides and calling the parent class methods.

class A:
    def foo(self) -> int:
        return 1

class B:
    def bar(self) -> int:
        return 2

class C( A, B ):
    def call_foo_bar(self) -> int:
        a = self.foo()
        a += self.bar()
        return a

    def foo(self) -> int:
        a = A.foo(self)
        a += 100
        return a

Gython supports Go’s typed maps.

a = map[string]int{
    'x': 1,
    'y': 2,
    'z': 3,
}

Gython supports array and map comprehensions. Below is an array of integers, and a map of strings with integer keys.

a = []int(x for x in range(3))
b = map[int]string{ i:'xxx' for i in range(10) }

Gython supports Go’s send data to channel syntax

a = go.channel( int )
a <- 1

Array and maps are always passed as pointers in a function call, this way the called function can modify the array or map inplace. In the example below a is typed as an array of integers []int, but it is actually retyped when transformed into Go as *[]int

def myfunc( a:[]int ):
    a.append( 100 )

x = []int()
myfunc( x )

Simple Generator Functions

Gython supports generator functions with a single for loop that yields from its main body. The generator function can also yield once before the loop, and once after.

def fib(n:int) -> int:
    int a = 0
    int b = 1
    int c = 0
    for x in range(n):
        yield a
        c = b
        b = a+b
        a = c
    yield -1

def main():
    arr = []int()
    for n in fib(20):
        arr.append( n )

Generic High Order Functions

Gython supports generic functions, where the first argument can be an instance of different subclasses. All the subclasses must share the same common base class. In the function definition the first argument is typed with the name of the common base class. In the function below my_generic, the first argument g is typed with the common base class: def my_generic( a:A )

class A:
    def __init__(self, x:int):
        int self.x = x

    def method1(self) -> int:
        return self.x

class B(A):

    def method1(self) ->int:
        return self.x * 2

class C(A):

    def method1(self) ->int:
        return self.x + 200


def my_generic( g:A ) ->int:
    return g.method1()

def main():
    a = A( 100 )
    b = B( 100 )
    c = C( 100 )

    x = my_generic( a )
    a.x == x

    y = my_generic( b )
    y==200

    z = my_generic( c )
    z==300

Metadata

Release files for gython 0.9.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gython 0.9.9
File Size Uploaded
gython-0.9.9.tar.gz 106.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gython 0.9.9
File Interpreter ABI Platform
gython-0.9.9-py3-none-any.whl Python 3 none any Details

Total release size: 198.0 kB

Release files / gython-0.9.9.tar.gz

Download URL gython-0.9.9.tar.gz
Size 106.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1f5987179735d80548d5ff09e33e3ec371b04e3e3dd536a1fafef2410256a0fd
BLAKE2b-256 checksum
How to use checksums
2bb628714caa46d3d08f98b8b0a3fe7b1959a67c6488986d5c6662a87b520ab4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / gython-0.9.9-py3-none-any.whl

Download URL gython-0.9.9-py3-none-any.whl
Size 91.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c7430b4e6d4d5ae59bec925310a4ce861f536c5afddee4add8f508407e59006c
BLAKE2b-256 checksum
How to use checksums
f45e4e4b0a200d38ae7be8abb92e2359324feeadd7e87eba4e9ed8fa02da938d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.9.9 This release

2 release files

0.9.8

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page