by Guest » 16 Feb 2025, 11:58
Ich möchte wissen, ob es die Möglichkeit gibt, abhängig vom Wert einer anderen Variablen einen variablen Typ festzulegen. , TCP oder UNIX -Socket), wenn eine Nachricht empfangen wird, können wir die Nachricht in einer so geschriebenen Funktion lesen: < /p>
Code: Select all
def _message_received(path: str, types: str, args: tuple):
# Each character of 'types' defines the type of args items
# for example if types == 'sifb'
# it means that 'args' is of type tuple[str, int, float, bytes]
...
< /code>
Natürlich kann ich Argumente so auspacken: < /p>
def _message_received(path: str, types: str, args: tuple):
if types == 'sif':
args: tuple[str, int, float]
value_str, value_int, value_f = args
< /code>
Ich frage mich, ob es möglich ist, diesen Vorgang automatisch zu automatisieren, beispielsweise eine Funktion < /p>
definierendef typed_args(args: tuple, types: str):
ret_args = []
for i in range(len(types)):
type_ = types[i]
arg = args[i]
if type_ == 's':
ret_args.append(str(arg))
elif type_ == 'f':
ret_args.append(float(arg))
elif type_ == 'i':
ret_args.append(int(arg))
return tuple(ret_args)
def _message_received(path: str, types: str, args: tuple):
if types == 'sif':
args = typed_args(args, types)
# of course here, pylance can't know the type of 'args'.
# I wonder if there is a way to force it to execute 'typed args'
# with the fact it knows that types == 'sif'
< /code>
Kann ich eine Funktion schreiben, damit Pylance die Arten der Variablen "Argumente" kennt? Alles, was ich im Moment wissen kann, ist, dass Args vom Typ Tupel ist [Str | int | float | Bytes]
, aber Pylance weder seine Länge noch die Arten seiner Tupelelemente. Es wäre bequem beim Auspacken.
Ich möchte wissen, ob es die Möglichkeit gibt, abhängig vom Wert einer anderen Variablen einen variablen Typ festzulegen. , TCP oder UNIX -Socket), wenn eine Nachricht empfangen wird, können wir die Nachricht in einer so geschriebenen Funktion lesen: < /p>
[code]def _message_received(path: str, types: str, args: tuple):
# Each character of 'types' defines the type of args items
# for example if types == 'sifb'
# it means that 'args' is of type tuple[str, int, float, bytes]
...
< /code>
Natürlich kann ich Argumente so auspacken: < /p>
def _message_received(path: str, types: str, args: tuple):
if types == 'sif':
args: tuple[str, int, float]
value_str, value_int, value_f = args
< /code>
Ich frage mich, ob es möglich ist, diesen Vorgang automatisch zu automatisieren, beispielsweise eine Funktion < /p>
definierendef typed_args(args: tuple, types: str):
ret_args = []
for i in range(len(types)):
type_ = types[i]
arg = args[i]
if type_ == 's':
ret_args.append(str(arg))
elif type_ == 'f':
ret_args.append(float(arg))
elif type_ == 'i':
ret_args.append(int(arg))
return tuple(ret_args)
def _message_received(path: str, types: str, args: tuple):
if types == 'sif':
args = typed_args(args, types)
# of course here, pylance can't know the type of 'args'.
# I wonder if there is a way to force it to execute 'typed args'
# with the fact it knows that types == 'sif'
< /code>
Kann ich eine Funktion schreiben, damit Pylance die Arten der Variablen "Argumente" kennt? Alles, was ich im Moment wissen kann, ist, dass Args vom Typ Tupel ist [Str | int | float | Bytes] [/code], aber Pylance weder seine Länge noch die Arten seiner Tupelelemente. Es wäre bequem beim Auspacken.