Value#
An opaque structure used to hold different types of values.
Before it can be used, a GValue has to be initialized to a specific type by
calling init on it.
Many types which are stored within a GValue need to allocate data on the
heap, so unset must always be called on a GValue to
free any such data once you’re finished with the GValue, even if the
GValue itself is stored on the stack.
The data within the structure has protected scope: it is accessible only
to functions within a TypeValueTable structure, or
implementations of the g_value_*() API. That is, code which implements new
fundamental types.
GValue users cannot make any assumptions about how data is stored
within the 2 element data union, and the g_type member should
only be accessed through the VALUE_TYPE macro and related
macros.
Methods#
- class Value
- dup_object() Object | None#
Get the contents of a
objectderivedValue, increasing its reference count. If the contents of theValueareNone, thenNonewill be returned.
- dup_variant() Variant | None#
Get the contents of a variant
Value, increasing its refcount. The returnedVariantis never floating.Added in version 2.26.
- fits_pointer() bool#
Determines if
valuewill fit inside the size of a pointer value.This is an internal function introduced mainly for C marshallers.
- get_char() int#
Do not use this function; it is broken on platforms where the %char type is unsigned, such as ARM and PowerPC. See
get_schar().Get the contents of a
intValue.Deprecated since version 2.32: This function’s return type is broken, see
get_schar()
- init(g_type: GType) Value#
Initializes
valueto store values of the giventype, and sets its value to the initial value fortype.This must be called before any other methods on a
Value, so the value knows what type it’s meant to store.GValue value = G_VALUE_INIT; g_value_init (&value, SOME_G_TYPE); … g_value_unset (&value);
- Parameters:
g_type – type the
Valueshould hold values of
- init_from_instance(instance: TypeInstance) None#
Initializes and sets
valuefrom an instantiatable type.This calls the [callback``GObject``.TypeValueCollectFunc] function for the type the
Valuecontains.Note: The
valuewill be initialised with the exact type ofinstance. If you wish to set thevalue’s type to a differentType(such as a parent class type), you need to manually callinitandset_instance.Added in version 2.42.
- Parameters:
instance – the instance
- peek_pointer() Any | None#
Returns the value contents as a pointer.
This function asserts that
fits_pointerreturned true for the passed in value.This is an internal function introduced mainly for C marshallers.
- reset() Value#
Clears the current value in
valueand resets it to the initial value (as if the value had just been initialized usinginit).
- set_boolean(v_boolean: bool) None#
Set the contents of a
boolValuetov_boolean.- Parameters:
v_boolean – boolean value to be set
- set_boxed_take_ownership(v_boxed: Any = None) None#
This is an internal function introduced mainly for C marshallers.
Deprecated since version 2.4: Use
take_boxed()instead.- Parameters:
v_boxed – duplicated unowned boxed value to be set
- set_char(v_char: int) None#
Set the contents of a
intValuetov_char.Deprecated since version 2.32: This function’s input type is broken, see
set_schar()- Parameters:
v_char – character value to be set
- set_double(v_double: float) None#
Set the contents of a
floatValuetov_double.- Parameters:
v_double – double value to be set
- set_enum(v_enum: int) None#
Set the contents of a
EnumValuetov_enum.- Parameters:
v_enum – enum value to be set
- set_flags(v_flags: int) None#
Set the contents of a
FlagsValuetov_flags.- Parameters:
v_flags – flags value to be set
- set_float(v_float: float) None#
Set the contents of a
floatValuetov_float.- Parameters:
v_float – float value to be set
- set_gtype(v_gtype: GType) None#
Set the contents of a
GTypeValuetov_gtype.Added in version 2.12.
- Parameters:
v_gtype –
Typeto be set
- set_instance(instance: Any = None) None#
Sets
valuefrom an instantiatable type.This calls the [callback``GObject``.TypeValueCollectFunc] function for the type the
Valuecontains.- Parameters:
instance – the instance
- set_int(v_int: int) None#
Set the contents of a
intValuetov_int.- Parameters:
v_int – integer value to be set
- set_int64(v_int64: int) None#
Set the contents of a
intValuetov_int64.- Parameters:
v_int64 – 64bit integer value to be set
- set_interned_string(v_string: str | None = None) None#
Set the contents of a
strValuetov_string. The string is assumed to be static and interned (canonical, for example fromintern_string()), and is thus not duplicated when setting theValue.Added in version 2.66.
- Parameters:
v_string – static string to be set
- set_long(v_long: int) None#
Set the contents of a
intValuetov_long.- Parameters:
v_long – long integer value to be set
- set_object(v_object: Object | None = None) None#
Set the contents of a
objectderivedValuetov_object.set_object()increases the reference count ofv_object(theValueholds a reference tov_object). If you do not wish to increase the reference count of the object (i.e. you wish to pass your current reference to theValuebecause you no longer need it), usetake_object()instead.It is important that your
Valueholds a reference tov_object(either its own, or one it has taken) to ensure that the object won’t be destroyed while theValuestill exists).- Parameters:
v_object – object value to be set
- set_param(param: ParamSpec | None = None) None#
Set the contents of a
ParamSpecValuetoparam.- Parameters:
param – the
ParamSpecto be set
- set_pointer(v_pointer: Any = None) None#
Set the contents of a pointer
Valuetov_pointer.- Parameters:
v_pointer – pointer value to be set
- set_schar(v_char: int) None#
Set the contents of a
intValuetov_char.Added in version 2.32.
- Parameters:
v_char – signed 8 bit integer to be set
- set_static_boxed(v_boxed: Any = None) None#
Set the contents of a
BoxedderivedValuetov_boxed.The boxed value is assumed to be static, and is thus not duplicated when setting the
Value.- Parameters:
v_boxed – static boxed value to be set
- set_static_string(v_string: str | None = None) None#
Set the contents of a
strValuetov_string. The string is assumed to be static, and is thus not duplicated when setting theValue.If the the string is a canonical string, using
set_interned_string()is more appropriate.- Parameters:
v_string – static string to be set
- set_string(v_string: str | None = None) None#
Set the contents of a
strValueto a copy ofv_string.- Parameters:
v_string – caller-owned string to be duplicated for the
Value
- set_string_take_ownership(v_string: str | None = None) None#
This is an internal function introduced mainly for C marshallers.
Deprecated since version 2.4: Use
take_string()instead.- Parameters:
v_string – duplicated unowned string to be set
- set_uchar(v_uchar: int) None#
Set the contents of a unsigned
intValuetov_uchar.- Parameters:
v_uchar – unsigned character value to be set
- set_uint(v_uint: int) None#
Set the contents of a unsigned
intValuetov_uint.- Parameters:
v_uint – unsigned integer value to be set
- set_uint64(v_uint64: int) None#
Set the contents of a unsigned
intValuetov_uint64.- Parameters:
v_uint64 – unsigned 64bit integer value to be set
- set_ulong(v_ulong: int) None#
Set the contents of a unsigned
intValuetov_ulong.- Parameters:
v_ulong – unsigned long integer value to be set
- set_variant(variant: Variant | None = None) None#
Set the contents of a variant
Valuetovariant. If the variant is floating, it is consumed.Added in version 2.26.
- Parameters:
variant – a
Variant, orNone
- take_boxed(v_boxed: Any = None) None#
Sets the contents of a
BoxedderivedValuetov_boxedand takes over the ownership of the caller’s reference tov_boxed; the caller doesn’t have to unref it any more.Added in version 2.4.
- Parameters:
v_boxed – duplicated unowned boxed value to be set
- take_string(v_string: str | None = None) None#
Sets the contents of a
strValuetov_string.Added in version 2.4.
- Parameters:
v_string – string to take ownership of
- take_variant(variant: Variant | None = None) None#
Set the contents of a variant
Valuetovariant, and takes over the ownership of the caller’s reference tovariant; the caller doesn’t have to unref it any more (i.e. the reference count of the variant is not increased).If
variantwas floating then its floating reference is converted to a hard reference.If you want the
Valueto hold its own reference tovariant, useset_variant()instead.This is an internal function introduced mainly for C marshallers.
Added in version 2.26.
- Parameters:
variant – a
Variant, orNone
- transform(dest_value: Value) bool#
Tries to cast the contents of
src_valueinto a type appropriate to store indest_value.If a transformation is not possible,
dest_valueis not modified.For example, this could transform a
G_TYPE_INTvalue into aG_TYPE_FLOATvalue.Performing transformations between value types might incur precision loss. Especially transformations into strings might reveal seemingly arbitrary results and the format of particular transformations to strings is not guaranteed over time.
- Parameters:
dest_value – target value
- classmethod type_compatible(src_type: GType, dest_type: GType) bool#
Checks whether a
copyis able to copy values of typesrc_typeinto values of typedest_type.- Parameters:
src_type – source type to be copied
dest_type – destination type for copying
- classmethod type_transformable(src_type: GType, dest_type: GType) bool#
Checks whether
transformis able to transform values of typesrc_typeinto values of typedest_type.Note that for the types to be transformable, they must be compatible or a transformation function must be registered using
register_transform_func.- Parameters:
src_type – source type
dest_type – target type