Are nested python try-except blocks bad code or code smell?












1














I have a situation where, I get an object, fillable_object which I need to fill out and I have another object, filler_object, that I use to fill up the first object. The filler_object however, can be of different types that don't have the same attributes. In which case I want the fields related to that in the fillable_object to get the value null.



What I thought of was to group for each class that fillable_object can be, in a try except block. Since the attribute sets of the different classes are mutually exclusive it will either get all of them or non, then try for the next type etc. Currently there aren't too many classes that fillable_object can have, but I can see it as being annoying to maintain later if the number increases.



def fill_with(fillable, filler):
try:
fillable.attr1 = filler.attr1
except AttrError:
try:
fillable.attr2 = filler.attr2
fillable.attr3 = filler.attr3
except AttrError:
try:
#Continue for each class filler can be.
except AttrError:
return fillable









share|improve this question






















  • To avoid nesting you can place a return at the end of each try block and a "pass" in the except. So the try blocks can be placed one after another.
    – Michael Butscher
    Nov 23 '18 at 14:44












  • Yea but if I do that it would be redundant. If the filler object is of one type it can't be of any of the others so I would do x assignments redundant and throw class_count of exceptions. It might not be much of a resource waste but still
    – BigBadCoder
    Nov 23 '18 at 15:28






  • 1




    looks like XY problem...
    – hkBst
    Nov 23 '18 at 18:03










  • @hkBst Could you please elaborate?
    – BigBadCoder
    Nov 23 '18 at 20:40






  • 1




    To test if a particular attribute is present, there is hasattr(). To test for the type/class of an object there are type() and isinstance().
    – Michael Butscher
    Nov 23 '18 at 23:41
















1














I have a situation where, I get an object, fillable_object which I need to fill out and I have another object, filler_object, that I use to fill up the first object. The filler_object however, can be of different types that don't have the same attributes. In which case I want the fields related to that in the fillable_object to get the value null.



What I thought of was to group for each class that fillable_object can be, in a try except block. Since the attribute sets of the different classes are mutually exclusive it will either get all of them or non, then try for the next type etc. Currently there aren't too many classes that fillable_object can have, but I can see it as being annoying to maintain later if the number increases.



def fill_with(fillable, filler):
try:
fillable.attr1 = filler.attr1
except AttrError:
try:
fillable.attr2 = filler.attr2
fillable.attr3 = filler.attr3
except AttrError:
try:
#Continue for each class filler can be.
except AttrError:
return fillable









share|improve this question






















  • To avoid nesting you can place a return at the end of each try block and a "pass" in the except. So the try blocks can be placed one after another.
    – Michael Butscher
    Nov 23 '18 at 14:44












  • Yea but if I do that it would be redundant. If the filler object is of one type it can't be of any of the others so I would do x assignments redundant and throw class_count of exceptions. It might not be much of a resource waste but still
    – BigBadCoder
    Nov 23 '18 at 15:28






  • 1




    looks like XY problem...
    – hkBst
    Nov 23 '18 at 18:03










  • @hkBst Could you please elaborate?
    – BigBadCoder
    Nov 23 '18 at 20:40






  • 1




    To test if a particular attribute is present, there is hasattr(). To test for the type/class of an object there are type() and isinstance().
    – Michael Butscher
    Nov 23 '18 at 23:41














1












1








1


0





I have a situation where, I get an object, fillable_object which I need to fill out and I have another object, filler_object, that I use to fill up the first object. The filler_object however, can be of different types that don't have the same attributes. In which case I want the fields related to that in the fillable_object to get the value null.



What I thought of was to group for each class that fillable_object can be, in a try except block. Since the attribute sets of the different classes are mutually exclusive it will either get all of them or non, then try for the next type etc. Currently there aren't too many classes that fillable_object can have, but I can see it as being annoying to maintain later if the number increases.



def fill_with(fillable, filler):
try:
fillable.attr1 = filler.attr1
except AttrError:
try:
fillable.attr2 = filler.attr2
fillable.attr3 = filler.attr3
except AttrError:
try:
#Continue for each class filler can be.
except AttrError:
return fillable









share|improve this question













I have a situation where, I get an object, fillable_object which I need to fill out and I have another object, filler_object, that I use to fill up the first object. The filler_object however, can be of different types that don't have the same attributes. In which case I want the fields related to that in the fillable_object to get the value null.



What I thought of was to group for each class that fillable_object can be, in a try except block. Since the attribute sets of the different classes are mutually exclusive it will either get all of them or non, then try for the next type etc. Currently there aren't too many classes that fillable_object can have, but I can see it as being annoying to maintain later if the number increases.



def fill_with(fillable, filler):
try:
fillable.attr1 = filler.attr1
except AttrError:
try:
fillable.attr2 = filler.attr2
fillable.attr3 = filler.attr3
except AttrError:
try:
#Continue for each class filler can be.
except AttrError:
return fillable






python functional-programming






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 14:30









BigBadCoder

5091419




5091419












  • To avoid nesting you can place a return at the end of each try block and a "pass" in the except. So the try blocks can be placed one after another.
    – Michael Butscher
    Nov 23 '18 at 14:44












  • Yea but if I do that it would be redundant. If the filler object is of one type it can't be of any of the others so I would do x assignments redundant and throw class_count of exceptions. It might not be much of a resource waste but still
    – BigBadCoder
    Nov 23 '18 at 15:28






  • 1




    looks like XY problem...
    – hkBst
    Nov 23 '18 at 18:03










  • @hkBst Could you please elaborate?
    – BigBadCoder
    Nov 23 '18 at 20:40






  • 1




    To test if a particular attribute is present, there is hasattr(). To test for the type/class of an object there are type() and isinstance().
    – Michael Butscher
    Nov 23 '18 at 23:41


















  • To avoid nesting you can place a return at the end of each try block and a "pass" in the except. So the try blocks can be placed one after another.
    – Michael Butscher
    Nov 23 '18 at 14:44












  • Yea but if I do that it would be redundant. If the filler object is of one type it can't be of any of the others so I would do x assignments redundant and throw class_count of exceptions. It might not be much of a resource waste but still
    – BigBadCoder
    Nov 23 '18 at 15:28






  • 1




    looks like XY problem...
    – hkBst
    Nov 23 '18 at 18:03










  • @hkBst Could you please elaborate?
    – BigBadCoder
    Nov 23 '18 at 20:40






  • 1




    To test if a particular attribute is present, there is hasattr(). To test for the type/class of an object there are type() and isinstance().
    – Michael Butscher
    Nov 23 '18 at 23:41
















To avoid nesting you can place a return at the end of each try block and a "pass" in the except. So the try blocks can be placed one after another.
– Michael Butscher
Nov 23 '18 at 14:44






To avoid nesting you can place a return at the end of each try block and a "pass" in the except. So the try blocks can be placed one after another.
– Michael Butscher
Nov 23 '18 at 14:44














Yea but if I do that it would be redundant. If the filler object is of one type it can't be of any of the others so I would do x assignments redundant and throw class_count of exceptions. It might not be much of a resource waste but still
– BigBadCoder
Nov 23 '18 at 15:28




Yea but if I do that it would be redundant. If the filler object is of one type it can't be of any of the others so I would do x assignments redundant and throw class_count of exceptions. It might not be much of a resource waste but still
– BigBadCoder
Nov 23 '18 at 15:28




1




1




looks like XY problem...
– hkBst
Nov 23 '18 at 18:03




looks like XY problem...
– hkBst
Nov 23 '18 at 18:03












@hkBst Could you please elaborate?
– BigBadCoder
Nov 23 '18 at 20:40




@hkBst Could you please elaborate?
– BigBadCoder
Nov 23 '18 at 20:40




1




1




To test if a particular attribute is present, there is hasattr(). To test for the type/class of an object there are type() and isinstance().
– Michael Butscher
Nov 23 '18 at 23:41




To test if a particular attribute is present, there is hasattr(). To test for the type/class of an object there are type() and isinstance().
– Michael Butscher
Nov 23 '18 at 23:41












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448546%2fare-nested-python-try-except-blocks-bad-code-or-code-smell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448546%2fare-nested-python-try-except-blocks-bad-code-or-code-smell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)