Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON, Classes with Overloaded Operators all parts 1,2,3 are in connection to each other so please answer ALL parts! 2. (5 pts) Complete the class

PYTHON, Classes with Overloaded Operators

all parts 1,2,3 are in connection to each other so please answer ALL parts! image text in transcribed

2. (5 pts) Complete the class c, which should implement semi-private variables (functioning much like attributes: attributes prefaced by double-underscore in Python) which were discussed in lecture (from the class review lecture notes). Here, any attributes defined in the_init_ method will be considered semi-private, usable only in methods in that class (except there is one loophole). Remember that o._dict_stores object o's namespace; inside one of C's methods refer to by self. dict__ Use this information in your solution. Submit your code with all methods as they appear in the download, except for_setattr _and__getattr Complete class C by writing the_setattr__and_getattr__methods. Assume below C that o - C) 1. The_init_ method can define any number of attributes. I can test your code with an_init_ that has a different body than the one included in the class (which sets two attributes a and b). As you will see below, _setattr_ will store these attributes in a special form (as the attributes private a and private, b). Then,-getattr-and-setattr-will automatically use these private attributes only when inside of methods defined inside class C. Other non-private attributes can be added/used outside of the methods define inside class C. 2. Write the setattr_ method to work as follows (a) raise the NameError exception whenever it is called with a name that explicitly starts with private_: e.g., writing self.private a - oro.private a - anywhere raises this exception (b) otherwise, when a self attribute is set in the_init_ _method, set the name prefixed by private_ to its value: e.g., for the init_supplied, the name private_a will be set to 1, and the name private b will be set to 2: o._dict _will be f 'private a': 1, 'private b': 2) (c) otherwise, if the name prefaced by private_ is already an attribute in the object i. if the attribute is being set in a method defined inside the class C, then set the name prefixed by private_ to its value: it is OK to set a privately defined attribute in any of C's methods ii. otherwise raise the NameError exception: it is not OK to set a privately defined attribute outside of C's defined methods (d) otherwise, just set just the name (with no prefix) to its value Write the-getattr method (remember, it is called if we try to access an attribute name that is not already in the object's namespace: if we define self.x in init it will be store in the object's namespace as private x; so if we access self.x or o.x Python won't find x in the object's namespace, so it will call_getattr_to try find it binding) to work as follows (a) if the name prefaced by private_ is already an attribute in the object 3. i. if the attribute is being gotten in a method defined inside the class C, return the value associated with the name prefixed by private_: it is OK to get a privately defined attribute in any of C's methods ii. otherwise raise the NameBrror exception: eit is not OK to get a privately defined attribute outside of C's defined methods 2. (5 pts) Complete the class c, which should implement semi-private variables (functioning much like attributes: attributes prefaced by double-underscore in Python) which were discussed in lecture (from the class review lecture notes). Here, any attributes defined in the_init_ method will be considered semi-private, usable only in methods in that class (except there is one loophole). Remember that o._dict_stores object o's namespace; inside one of C's methods refer to by self. dict__ Use this information in your solution. Submit your code with all methods as they appear in the download, except for_setattr _and__getattr Complete class C by writing the_setattr__and_getattr__methods. Assume below C that o - C) 1. The_init_ method can define any number of attributes. I can test your code with an_init_ that has a different body than the one included in the class (which sets two attributes a and b). As you will see below, _setattr_ will store these attributes in a special form (as the attributes private a and private, b). Then,-getattr-and-setattr-will automatically use these private attributes only when inside of methods defined inside class C. Other non-private attributes can be added/used outside of the methods define inside class C. 2. Write the setattr_ method to work as follows (a) raise the NameError exception whenever it is called with a name that explicitly starts with private_: e.g., writing self.private a - oro.private a - anywhere raises this exception (b) otherwise, when a self attribute is set in the_init_ _method, set the name prefixed by private_ to its value: e.g., for the init_supplied, the name private_a will be set to 1, and the name private b will be set to 2: o._dict _will be f 'private a': 1, 'private b': 2) (c) otherwise, if the name prefaced by private_ is already an attribute in the object i. if the attribute is being set in a method defined inside the class C, then set the name prefixed by private_ to its value: it is OK to set a privately defined attribute in any of C's methods ii. otherwise raise the NameError exception: it is not OK to set a privately defined attribute outside of C's defined methods (d) otherwise, just set just the name (with no prefix) to its value Write the-getattr method (remember, it is called if we try to access an attribute name that is not already in the object's namespace: if we define self.x in init it will be store in the object's namespace as private x; so if we access self.x or o.x Python won't find x in the object's namespace, so it will call_getattr_to try find it binding) to work as follows (a) if the name prefaced by private_ is already an attribute in the object 3. i. if the attribute is being gotten in a method defined inside the class C, return the value associated with the name prefixed by private_: it is OK to get a privately defined attribute in any of C's methods ii. otherwise raise the NameBrror exception: eit is not OK to get a privately defined attribute outside of C's defined methods

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

Database Design And Implementation

Authors: Shouhong Wang, Hai Wang

1st Edition

1612330150, 978-1612330150

More Books

Students also viewed these Databases questions

Question

What behaviors are discouraged?

Answered: 1 week ago

Question

What is the principle of thermodynamics? Explain with examples

Answered: 1 week ago