From c4261ca853dcd1134fddc51f5515f4034bd88b25 Mon Sep 17 00:00:00 2001 From: Ken Jones <165197230+KenJones-Modular@users.noreply.github.com> Date: Mon, 13 Jan 2025 19:01:14 -0800 Subject: [PATCH] Added links to API references and fixed whitespace in code samples MODULAR_ORIG_COMMIT_REV_ID: 08f97ad1651fd8940c9d1e8bc6681ffb1460e007 --- docs/manual/get-started.mdx | 10 ++++++---- docs/manual/python/types.mdx | 30 ++++++++++++++++-------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/docs/manual/get-started.mdx b/docs/manual/get-started.mdx index 9f27b5d5cc..8540761f2e 100644 --- a/docs/manual/get-started.mdx +++ b/docs/manual/get-started.mdx @@ -344,7 +344,7 @@ def main(): ## 5. Create and use a function to print the grid Now let's create a function to generate a string representation of the game grid -that we can print it to the terminal. +that we can print to the terminal. There are actually two different keywords that we can use to define functions in Mojo: `def` and `fn`. Using `fn` gives us finer level control over the function @@ -362,7 +362,7 @@ def grid_str(rows: Int, cols: Int, grid: List[List[Int]]) -> String: # Iterate through rows 0 through rows-1 for row in range(rows): - # Iterate through columns 0 through cols-1 + # Iterate through columns 0 through cols-1 for col in range(cols): if grid[row][col] == 1: str += "*" # If cell is populated, append an asterisk @@ -540,7 +540,7 @@ def grid_str(grid: Grid) -> String: # Iterate through rows 0 through rows-1 for row in range(grid.rows): - # Iterate through columns 0 through cols-1 + # Iterate through columns 0 through cols-1 for col in range(grid.cols): if grid.data[row][col] == 1: str += "*" # If cell is populated, append an asterisk @@ -876,7 +876,9 @@ respectively. This function actually returns a value of type `Int64`, which is a signed 64-bit integer value. As described in [Numeric types](/mojo/manual/types#numeric-types), this is *not* the same as the `Int` type whose precision is dependent on the native word size of the system. -Therefore we're passing this value to the built-in +Therefore we're passing this value to the +[`Int()`](/mojo/stdlib/builtin/int/Int/#__init__) constructor, which explicitly +converts a numeric value to an `Int`. The return type of the method is `Self`, which is an alias for the type of the struct. This is a convenient shortcut if the actual name of the struct is long diff --git a/docs/manual/python/types.mdx b/docs/manual/python/types.mdx index 90f75fb866..4aab0ace57 100644 --- a/docs/manual/python/types.mdx +++ b/docs/manual/python/types.mdx @@ -145,24 +145,26 @@ Some Mojo APIs handle `PythonObject` just fine, but sometimes you'll need to explicitly convert a Python value into a native Mojo value. Currently `PythonObject` conforms to the -[`Intable`](/mojo/stdlib/builtin/int/Intable), -[`Stringable`](/mojo/stdlib/builtin/str/Stringable), and -[`Boolable`](/mojo/stdlib/builtin/bool/Boolable) traits, which means you can -convert Python values to Mojo `String`, and `Bool` types using the -[`str()`](/mojo/stdlib/builtin/str/str) and -[`bool()`](/mojo/stdlib/builtin/bool/bool-function) functions, construct an -`Int` using `Int(python_object)`, and print Python values using the built-in -[`print()`](/mojo/stdlib/builtin/io/print) function. - -`PythonObject` also provides the -[`to_float64()`](/mojo/stdlib/python/python_object/PythonObject#to_float64) for -converting to a Mojo floating point value. +[`Stringable`](/mojo/stdlib/builtin/str/Stringable), +[`Boolable`](/mojo/stdlib/builtin/bool/Boolable), +[`Floatable`](/mojo/stdlib/builtin/floatable/Floatable/), and +[`Intable`](/mojo/stdlib/builtin/int/Intable) traits, which means you can +convert Python values to Mojo `String`, `Bool`, and `Float64` types using the +[`str()`](/mojo/stdlib/builtin/str/str), +[`bool()`](/mojo/stdlib/builtin/bool/bool-function) and +[`float()`](/mojo/stdlib/builtin/floatable/float/) built-in functions, and +construct an `Int` using the [`Int()`](/mojo/stdlib/builtin/int/Int/#__init__) +constructor. `PythonObject` also conforms to the +[`Writable`](/mojo/stdlib/utils/write/Writable) trait so that you can print +Python values using the built-in [`print()`](/mojo/stdlib/builtin/io/print) +function. ```mojo -var i: Int = Int(py_int) var s: String = str(py_string) var b: Bool = bool(py_bool) -var f: Float64 = py_float.to_float64() +var f: Float64 = float(py_float) +var i: Int = Int(py_int) +print(py_obj) ``` ### Comparing Python types in Mojo