in Education by
More specifically, I want to be able to support lambda: , but I want to keep the code clear and to a concise. I have to validate the value, so I need a setter of some kind. I need to use lambda because I need to pass callbacks to Tkinter events. I also need to be able to modify the value of the attribute outside a binding. In my following examples, assume that a button widget called spam_button has been declared globally. Also asume that the class Eggs will have at least 10 attributes than need to all be accessed the same way (I like consistency). The first possible way I could do this is using just getters and setters: class Eggs(object): def __init__(self): self._spam = '' self.set_spam('Monster') print self.get_spam() spam_button.bind('', lambda: self.set_spam('Ouch')) def set_spam(self, spam): if len(spam) <= 32: self._spam = spam def get_spam(self): return self._spam But if I use this method and have lots of attributes, the code may get too long to manage. The second way I could do this is use properties, and use the setter in the callback: class Eggs(object): def __init__(self): self._spam = '' self.spam = 'Monster' print self.spam spam_button.bind('<Enter>', lambda: self.set_spam('Ouch')) def set_spam(self, spam): if len(spam) <= 32: self._spam = spam def get_spam(self): return self._spam spam = property(get_spam, set_spam) This way is a bit simpler than the first when accessing the attribute directly, but is inconsistent when using lambda. The third way to do this would be to make an additional class with get and set methods: class Spam(object): def __init__(self): self._value = '' def set(self, value): if len(spam) <= 32: self._value = value def get(self): return self._value class Eggs(object): def __init__(self): self._spam = '' self.spam = Spam() self.spam.set('Monster') print self.spam.get() spam_button.bind('<Enter>', lambda: self.spam.set('Ouch')) This method is more organised than the first, but I will need to make a class for each type of validation. The last way I might do it is use methods instead of properties (properties were the second example): class Eggs(object): def __init__(self): self._spam = '' self.spam('Monster') print self.spam() spam_button.bind('', lambda: self.spam('Ouch')) def spam(self, spam=None): if spam != None: if len(spam) <= 32: self._spam = spam else: return self._spam This method will probably be the shortest, but it is harder at a glance to tell whether I am getting or setting. Which (if any) of these methods are preferred? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Use a closure to return a function to be used as a callback, and define each condition as a function (using lambda or def, your preference) instead of each setter as a function. class Eggs(object): def __init__(self): self.spam = 'Monster' def spam_condition(string = None): return (string is not None) and (len(string) <= 32) self.spam_setter = self.set('spam', 'Ouch', spam_condition) spam_button.bind('<Enter>', self.spam_setter) self.spam_setter(val='Horse') def set(self, name, value, condition): def setter(val): if type(val) is not .... : # fill this in with the TYPE of the event value = val if condition(value): setattr(self, name, value) return setter Edit: Now you can call the setter, which will check the condition, from anywhere. Edit 2: This way, the setter will check if it got an event, and if not, use the value it's passed. You could also use: if not isinstance(val, ....) : # fill this in with the CLASS of the event

Related questions

0 votes
    More specifically, I want to be able to support lambda: , but I want to keep the code clear and to a concise. I have ... .set_spam('Ouch')) def set_spam(self, spam): if len(spam)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    I have a code: ab = 'off' if b.page == 'blog': ab = 'on' print ab Is there any other way to make it shorter in Python? Select the correct answer from above options...
asked Jan 25, 2022 in Education by JackTerrance
0 votes
    I have a very long query. I would like to split it in several lines in Python. A way to do it in JavaScript ... '\ 'def_id=' + def_id Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Which of following language is most preferred for AWS Lambda service ? python Node.js java Go Select the correct answer from above options...
asked Dec 13, 2021 in Education by JackTerrance
0 votes
    In a B+-tree index ______ for each value, we would normally maintain a list of all records with ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
0 votes
    I have created lambda function on node.js which returns JSON. It connected with API Gateway and worked well ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    A web page displays 10 items per page and has pagination enabled. What would be the recommended way to enable efficient ... (d)Hit the database every time user moves to next page...
asked Oct 6, 2020 in Technology by JackTerrance
0 votes
    Why lambda expression is to be used?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    What kind of variable you can access in an lambda expression??...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    Bitcoin network would make it impossible to bypass the traditional financial intermediaries. A. True B. False...
asked Nov 30, 2022 in Education by JackTerrance
0 votes
    What would be your preferred way to concatenate strings from a sequence such that between every two consecutive ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    If I want to hold a Teams meeting and invite “guests,” what happens if I select that only specific domains ... still able to join the Teams meeting from via a link?...
asked Mar 10, 2021 in Technology by JackTerrance
0 votes
    Apparently, Microsoft has not yet made WebGL work in Internet Explorer by default. Plus, Opera have it only ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 15, 2022 in Education by JackTerrance
...