Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Object Equality As experienced in the previous section of the lab, checking object equality by comparing each field individually is tedious. Though individual attribute comparisons

image text in transcribed

Object Equality As experienced in the previous section of the lab, checking object equality by comparing each field individually is tedious. Though individual attribute comparisons are necessary to properly test the init function, once object creation is known to work we would prefer to compare objects for equality in a simpler manner. This can be done by defining the e function within a class. We will use a simple definionf eq to reduce the tedium of writing test cases (more sophisticated checks may be introduced in CPE 102) When an object is compared to another value using, the default behavior is check if the two values are actually the same object (i.e., this is typically referred to as reference equality because both operands must refer to the same object for the check to return True) Instead, if you define the q function in the object's class, then that function will be called whenis used. As such, theeq can define what it means for the object to be equal to some other value (e.g., they may be considered equal only when each of their attributes is equal) Copy your objects.py file into the object_equality directory. Modify the definition of the Point class to add (a simplifiedqfunction. This function will take two arguments: self (the target of the call, much like with init and other (the other operand). The function must return True when the x and y attributes in self are equal to the x and y attributes in other. Your function will assume that other has these attributes. The attributes for a Point are typically of a floating point type. As such, you should use the epsilon_equal defined in utility.py when writing your eq function. We will discuss this function in class or lab. Modify object-equality-tests. py to test that your implementation of-eq-behaves as expected. You can do so by using assertEqual to compare two Point objects. Note that defining_ eqdoes not change the behavior of the !-operator. To do this you must define the ne function for the class. Doing so is not required for this lab, but you might try defining ne once you have completed the required portions of the lab. Object Equality As experienced in the previous section of the lab, checking object equality by comparing each field individually is tedious. Though individual attribute comparisons are necessary to properly test the init function, once object creation is known to work we would prefer to compare objects for equality in a simpler manner. This can be done by defining the e function within a class. We will use a simple definionf eq to reduce the tedium of writing test cases (more sophisticated checks may be introduced in CPE 102) When an object is compared to another value using, the default behavior is check if the two values are actually the same object (i.e., this is typically referred to as reference equality because both operands must refer to the same object for the check to return True) Instead, if you define the q function in the object's class, then that function will be called whenis used. As such, theeq can define what it means for the object to be equal to some other value (e.g., they may be considered equal only when each of their attributes is equal) Copy your objects.py file into the object_equality directory. Modify the definition of the Point class to add (a simplifiedqfunction. This function will take two arguments: self (the target of the call, much like with init and other (the other operand). The function must return True when the x and y attributes in self are equal to the x and y attributes in other. Your function will assume that other has these attributes. The attributes for a Point are typically of a floating point type. As such, you should use the epsilon_equal defined in utility.py when writing your eq function. We will discuss this function in class or lab. Modify object-equality-tests. py to test that your implementation of-eq-behaves as expected. You can do so by using assertEqual to compare two Point objects. Note that defining_ eqdoes not change the behavior of the !-operator. To do this you must define the ne function for the class. Doing so is not required for this lab, but you might try defining ne once you have completed the required portions of the lab

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions