Category : attributes

I’ve recently discovered function attributes in python. Is it good practice to use those attributes in closure and nested functions, especially changing those attributes from the inner function? Is it ‘better’ than using nonlocal? For example- def combination_lock(*args): combination_lock.index = 0 code = [*args] combination_lock.state = True def inner(num): if code[combination_lock.index] != num: combination_lock.state = ..

Read more

I’m trying to get the following code to work: class Test: all = list() last = None def __init__(self): pass def new(self, object): self.object = object self.all.append(self) self.last = self return self def get(self): print(self.object) testing = Test() testing.new(‘Hello1’) testing.new(‘Hello2’) testing.new(‘Hello3’) print(testing.all[0]) print(testing.all[1].last) print(testing.all[1].object) print(testing.all[2].object) Result: <__main__.Test object at 0x7f23fee1d040> None Hello3 Hello3 Expected: <__main__.Test ..

Read more

I have 2 child classes executing the same code but on different conditions. class Child1(Parent): … def updateChart(self): if self.value % 15 == 0: self.value += 5 class Child2(Parent): … def updateChart(self): if self.value % 30 == 0: self.value += 5 Is there anyway to move the method itself to the parent class but with ..

Read more

Below is all the code I have so far. When I run it, I’m told that resistivity is not an attribute of the material class despite it being right there. When I change resistivity to conductivity in the str method everything works fine but for the life of me I can’t figure this out. What ..

Read more

I have this code. However I am not able to run it as a function. def verify_result_same(): if verify_result_same.is_identical() & verify_result_same.confidence() > 0.5: face_client.face.verify_face_to_face(source_image1_id, detected_faces_ids[0]) print(‘Faces from {} & {} are of the same person, with confidence: {}’ .format(source_image_file_name1, target_image_file_names[0], verify_result_same.confidence)) print("Valid face verification") else: print(‘Faces from {} & {} are of a different person, ..

Read more

Using the code: class Values(): # class for storing values value_1 = None print(Values.value_1) print(getattr(Values, ‘value_1’)) I am able to get Output: None None But when trying to use this code it does not work for any of them: print(Values.__dict__.get(str(value_1))) print(getattr(Values, str(value_1))) print(Values.__dict__[str(value_1)]) I get Output: NameError: name ‘value_1’ is not defined I am trying ..

Read more

how do i write a program that has a class named Kitchen which has 3 attributes of size, num_of_chefs, and name_of_chefs. It will also have a function called start_cooking which doesn’t require any additional parameters and will print a statement that says that The num_of_chefs chefs have started cooking (if there are 8 then it ..

Read more