Pythonic way to assign default values?

You need to decide what you are trying achieve here. The use of the word "error" is probably misleading.

You need to decide what you are trying achieve here. The use of the word "error" is probably misleading. If you are actually trying to handle the case where the wrong type of object is passed to your function then you don't want to handle that and should raise an exception.

If you are trying to allow your function to be used on a series of different types then that's not really an error and using a default value may be reasonable. The simplest option is to test whether the attribute exists first. For example: if hasattr(lst, "attr"): attr = lst.

Attr else: attr = {} I'm assuming the lst. Attr is a dictionary, in which case you can handle the default value like so: lst.attr. Get(idx, default_value) Never use a try/except statement where you don't specify what exception you are catching.

You can end up masking much more than you intended to. With your final piece of code I think you should not try and solve it in a single line. Readability counts.

I'm not happy with the code below, but it would be improved if x, y and attr were replaced with more descriptive names. Attrs = (x. Attr if hasattr(x) else {}) for x in y print attr.

Get(idx, default_value) for attr in attrs.

I wouldn't use hasattr - it uses try/except under the hood. Simply replace the if statement with try: and else: with except AttributeError:. Certainly in line with the EAFP philosophy.

– Wayne Werner Nov 30 at 16:58.

Import this ... Explicit is better than implicit. ... Sparse is better than dense. Readability counts.

... Errors should never pass silently. Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess. I have the feeling that the "Pythonic way to assign default values"* is either to handle exception - as you already mentioned in your question - either to write you own getters.

Write a function and make it smart enough: def get_attr_with_index_and_default(obj, attr_name, index, default): try: return getattr(obj, attr_name)index except (AttributeError, IndexError): return default print get_attr_with_index_and_default(x, 'attr', idx, some_default) for x in y if you control the class of x, you can use this as a method, or adapt it as a a descriptor, but imo this is not worth it and will result in obscure code and difficult to trace bugs.

Even if there exists one-liner, it would be really complicated. E.g. This one still doesn't handle index problem: some_value = getattr(lst, 'attr', {idx: default_value})idx I would suggest to write some getter for your lst.

When there is 'attr' in object class C(object): attr = "attr on class" lst = C() print lst. Attr if hasattr(lst,'attr') else "default value" When there is no attribute 'attr' class C(object): #attr = "attr on class" pass lst = C() print lst. Attr if hasattr(lst,'attr') else "default value.

1 was about to post this – soulcheck Nov 30 at 11:01 @soulcheck you're late :) – Gagandeep Singh Nov 30 at 11:16.

There is no simple, elegant solution to your problem. If it must be done using a one-liner that can be used in list-comprehensions for example, then you could do the following: # If lst. Attr is a dict.

Some_value = getattr(lst, 'attr', {}). Get(idx, default_value) # OR some_value = lst.attr. Get(idx, default_value) if hasattr(lst, 'attr') else default_value # OR some_value = lst.

Attridx if hasattr(lst, 'attr') and idx in lst. Attr else default_value # If lst. Attr is a sequence.

Some_value = lst. Attridx if idx Attr) else default_value.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions