tiny.python.Value
Defined in object.value.
One Python value, as a tagged union of thirteen kinds.
API (18)
Actions
Public operations.
eql: Returns whether two values are equal by Python's rules, for==,!=, membership tests and dictionary lookups.hash: Returns a 64-bit Wyhash of the value, with a fixed seed, so a value hashes the same in every run.hashable: Returns whether the value may be a dictionary key, so the virtual machine checks it before every dictionary lookup and insertion.integerLike: Returns the integer a value stands for: an integer's own value, 1 forTrue, 0 forFalse, andnullfor every other kind.truthy: Returns whether the value is true by Python's truth rules, forif,while,and,orandnot.
Fields and members
Public fields and members.
Source
Source: lib/python/src/object/value.zig:39
zig
/// One Python value, as a tagged union of thirteen kinds. The virtual machine reads and writes/// values in every operation, and `Result.value` is one. `None`, booleans and integers live in the/// value itself, a string is a slice of bytes held elsewhere, and every other kind points to an/// object held elsewhere. The objects that the virtual machine makes live on its heap. Copying a/// value copies the pointer, so both copies refer to the same object.pub const Value = union(enum) { /// Python's `None`, with no payload. `None` is false by Python's truth rules and equal only to /// `None`. none, /// Python's `True` or `False`. A boolean counts as the integer 1 or 0 in arithmetic, equality, /// ordering and hashing, so `True == 1` holds and a dictionary finds the key `1` under `True`. /// A boolean is never the same object as an integer under `is`. boolean: bool, /// A Python integer, stored in 128 signed bits. Arithmetic that leaves that range fails with /// `IntegerOverflow`. integer: i128, /// A Python string, as a slice of UTF-8 bytes that the value borrows. A string literal and its /// slices with step 1 point into the source text, and a string the run built points into the /// heap. Two strings are equal when their bytes are equal. Two strings are the same object /// under `is` when they start at the same address and have the same length. string: []const u8, /// A Python function, as the position of its body in the function table of the top-level chunk. /// The position names nothing once that chunk is freed. function: usize, /// One of the nine builtin functions, named by a `Builtin` tag. builtin: Builtin, /// A list or dictionary method bound to the object it was read from, as a pointer to a /// `NativeMethod` on the heap. method: *NativeMethod, /// A pointer to a Python list. When the virtual machine made the list, the machine's heap owns /// it. list: *List, /// A pointer to a Python tuple. When the virtual machine made the tuple, the machine's heap /// owns it. tuple: *Tuple, /// A pointer to a Python dictionary. When the virtual machine made the dictionary, the /// machine's heap owns it. dict: *Dict, /// A pointer to a view of a dictionary's keys, values or items. view: *DictView, /// A pointer to a Python range. range: *Range, /// A pointer to an iterator, whose position advances as it yields items. iterator: *Iterator, /// Returns whether the value is true by Python's truth rules, for `if`, `while`, `and`, `or` /// and `not`. `None`, `False`, zero, and empty strings, lists, tuples, dictionaries, dictionary /// views and ranges are false. A dictionary view is false when its dictionary is empty. /// Functions, builtins, bound methods and iterators are always true. pub fn truthy(self: Value) bool { return switch (self) { .none => false, .boolean => |value| value, .integer => |value| value != 0, .string => |value| value.len != 0, .function => true, .builtin => true, .method => true, .list => |value| value.items.len != 0, .tuple => |value| value.items.len != 0, .dict => |value| value.entries.items.len != 0, .view => |value| value.dict.entries.items.len != 0, .range => |value| value.length != 0, .iterator => true, }; } /// Returns the integer a value stands for: an integer's own value, 1 for `True`, 0 for `False`, /// and `null` for every other kind. Arithmetic, indexing, `range`, `enumerate` and repetition /// read their integer inputs through it. pub fn integerLike(self: Value) ?i128 { return switch (self) { .integer => |value| value, .boolean => |value| if (value) 1 else 0, .none => null, .string => null, .function => null, .builtin => null, .method => null, .list => null, .tuple => null, .dict => null, .view => null, .range => null, .iterator => null, }; } /// Returns whether the value may be a dictionary key, so the virtual machine checks it before /// every dictionary lookup and insertion. `None`, booleans, integers, strings, functions, /// builtins, bound methods and ranges may be keys. A tuple may be a key when every item may. /// Lists, dictionaries, dictionary views and iterators may never be keys. The check walks a /// tuple item by item, so its cost grows with the tuple. pub fn hashable(self: Value) bool { return switch (self) { .none, .boolean, .integer, .string, .function, .builtin, .method, .range => true, .tuple => |value| sequenceHashable(value.items), .list, .dict, .view, .iterator => false, }; } /// Returns whether two values are equal by Python's rules, for `==`, `!=`, membership tests and /// dictionary lookups. Integers and booleans compare by their integer value. Strings compare /// their bytes, and lists and tuples compare item by item. Two dictionaries are equal when they /// hold equal values under the same keys, in any order. Two ranges are equal when they yield /// the same elements. Keys views compare their dictionaries' keys, items views compare their /// dictionaries, and a values view equals only itself. Two bound methods are equal when they /// have the same method and the same receiver. Functions and builtins compare their positions /// and tags, and iterators are equal only to themselves. Values of unrelated kinds are unequal, /// and the comparison returns no error. Comparing a list with itself returns true before /// comparing its items. Comparing a tuple with itself returns true before comparing its items. /// A list containing itself therefore equals itself, including when it holds a tuple containing /// that same list. Two distinct lists or dictionaries that each contain themselves recurse /// until the native stack overflows when comparison reaches those self-references because /// comparison has no depth bound. pub fn eql(self: Value, other: Value) bool { if (self.integerLike()) |left| { if (other.integerLike()) |right| return left == right; } return switch (self) { .none => other == .none, .boolean => false, .integer => false, .string => |left| switch (other) { .string => |right| std.mem.eql(u8, left, right), else => false, }, .function => |left| switch (other) { .function => |right| left == right, else => false, }, .builtin => |left| switch (other) { .builtin => |right| left == right, else => false, }, .method => |left| switch (other) { .method => |right| nativeMethodsEqual(left, right), else => false, }, .list => |left| switch (other) { .list => |right| listsEqual(left, right), else => false, }, .tuple => |left| switch (other) { .tuple => |right| left == right or sequencesEqual(left.items, right.items), else => false, }, .dict => |left| switch (other) { .dict => |right| dictsEqual(left, right), else => false, }, .view => |left| switch (other) { .view => |right| dictViewsEqual(left, right), else => false, }, .range => |left| switch (other) { .range => |right| rangesEqual(left, right), else => false, }, .iterator => |left| switch (other) { .iterator => |right| left == right, else => false, }, }; } /// Returns a 64-bit Wyhash of the value, with a fixed seed, so a value hashes the same in every /// run. The dictionary's hash table hashes keys with it. Equal values hash equally: an integer /// and the boolean with its value share a hash, and so do equal ranges and equal tuples. A /// bound method hashes its method and its receiver's address. The function asserts that the /// value is hashable, so the caller checks `hashable` first. pub fn hash(self: Value) u64 { std.debug.assert(self.hashable()); var hasher = std.hash.Wyhash.init(0x5059_5448_4f4e_5641); hashInto(&hasher, self); return hasher.final(); }};Source: lib/python/src/root.zig:95
zig
pub const Value = object.Value;Also reachable as
Audit
| Definitions | 6 |
|---|---|
| Public names | 18 |
| Members | 13 |
| Version | 26.7.0 |
| Revision | daab053ee433 |