Structs in Assembly Language

Based on your input, here’s the translated code and explanation from Go to Python in Markdown format:

Our example will demonstrate the usage of structs, which are typed collections of fields. They’re useful for grouping data together to form records.

This Person class type has name and age fields.

class Person:
    def __init__(self, name: str):
        self.name = name
        self.age = 42

The new_person function constructs a new Person instance with the given name.

def new_person(name: str) -> Person:
    return Person(name)

Python has automatic garbage collection, so objects are cleaned up when there are no active references to them.

This syntax creates a new Person instance.

print(Person("Bob").name, Person("Bob").age)

You can name the fields when initializing a Person.

alice = Person("Alice")
alice.age = 30
print(alice.name, alice.age)

Omitted fields will be set to their default values.

fred = Person("Fred")
print(fred.name, fred.age)

You can create a reference to a Person instance.

ann = Person("Ann")
ann.age = 40
print(ann.name, ann.age)

It’s idiomatic to encapsulate new instance creation in constructor functions.

print(new_person("Jon").name, new_person("Jon").age)

Access class fields with a dot.

sean = Person("Sean")
sean.age = 50
print(sean.name, sean.age)

You can also use dots with class references - the references are automatically dereferenced.

sp = sean
print(sp.age)

Classes are mutable.

sp.age = 51
print(sp.age)

If a class type is only used for a single value, we don’t have to give it a name. The value can have an anonymous class type.

dog = type("Dog", (object,), {"name": "Rex", "isGood": True})()
print(dog.name, dog.isGood)

Next example: Methods.