variable-assignment

Instance method to return name of attribute to which it assigns a value

Instance method to return name of attribute to which it assigns a value Question: Any way to return name of instance attribute in method it calls on assignment? class MyClass: def __init__(self): self.not_relevant = 0 self.name_to_return = self.assign_own_name() def assign_own_name(self): return "name_to_return" # to replace assert MyClass().name_to_return == ‘name_to_return’ Use case: multiple attribute are initialized …

Total answers: 1

Updating multiple Pydantic fields that are validated together

Updating multiple Pydantic fields that are validated together Question: How do you update multiple properties on a pydantic model that are validated together and dependent upon each other? Here is a contrived but simple example: from pydantic import BaseModel, root_validator class Example(BaseModel): a: int b: int @root_validator def test(cls, values): if values[‘a’] != values[‘b’]: raise …

Total answers: 2

why do i keep getting this error and how do i solve it

why do i keep getting this error and how do i solve it Question: So i have this massive python code but i cant get it to work. It keeps saying: UnboundLocalError: local variable ‘secondnum’ referenced before assignment This problem is actually solved now thanks for the help but the program still doesnt seem to …

Total answers: 2

Reassigning variable in Python

Reassigning variable in Python Question: In Python, I have an expensive function a(x) which is currently evaluated multiple times within another function. At the start of that function, I want to evaluate a(x) once, and reassign the variable name a locally to that value. But I keep getting an Unbound Local Error. As a MWE: …

Total answers: 4

Is it possible to get an error when accidentally assigned wrong type in python?

Is it possible to get an error when accidentally assigned wrong type in python? Question: For example I have next code: name = str(‘John Doe’) Let’s imagine after I assign to name = 1, but code still valid. Is it possible to get an error in this case in Python or within some special tool? …

Total answers: 2

Double assignment of same variable in one expression in Python – does it have any purpose?

Double assignment of same variable in one expression in Python – does it have any purpose? Question: I was going through the delta lake documentation page. There is a line like this : from delta import * builder = pyspark.sql.SparkSession.builder.appName("MyApp") .config("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension") .config("spark.sql.catalog.spark_catalog", "org.apache.spark.sql.delta.catalog.DeltaCatalog") spark = spark = configure_spark_with_delta_pip(builder).getOrCreate() In the last line, we see …

Total answers: 1

Unexpected behaviour with a conditional generator expression

Unexpected behaviour with a conditional generator expression Question: I was running a piece of code that unexpectedly gave a logic error at one part of the program. When investigating the section, I created a test file to test the set of statements being run and found out an unusual bug that seems very odd. I …

Total answers: 8

python atomic data types

python atomic data types Question: It was written here that Python has both atomic and reference object types. Atomic objects are: int, long, complex. When assigning atomic object, it’s value is copied, when assigning reference object it’s reference is copied. My question is: why then, when i do the code bellow i get ‘True’? a …

Total answers: 4

Difference between a -= b and a = a – b in Python

Difference between a -= b and a = a – b in Python Question: I have recently applied this solution for averaging every N rows of matrix. Although the solution works in general I had problems when applied to a 7×1 array. I have noticed that the problem is when using the -= operator. To …

Total answers: 3

How to assign a value to a TensorFlow variable?

How to assign a value to a TensorFlow variable? Question: I am trying to assign a new value to a tensorflow variable in python. import tensorflow as tf import numpy as np x = tf.Variable(0) init = tf.initialize_all_variables() sess = tf.InteractiveSession() sess.run(init) print(x.eval()) x.assign(1) print(x.eval()) But the output I get is 0 0 So the …

Total answers: 8