Don't surprise the programmer by behaving/working differently than expected.
Questions tagged [least-astonishment]
15 questions
2858
votes
32 answers
"Least Astonishment" and the Mutable Default Argument
Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue:
def foo(a=[]):
a.append(5)
return a
Python novices would expect this function to always return a list with only one element: [5]. The…
Stefano Borini
- 125,999
- 87
- 277
- 404
28
votes
3 answers
Why is negative id or zero considered a bad practice?
Why is negative id or zero considered a bad practice when inserting a primary key in a database table?
I think it could be useful in some cases, but people say that it is not recommended, despite the fact that they never say/know why.
So, I was…
falsarella
- 11,640
- 8
- 65
- 104
11
votes
5 answers
"Boolean" operations in Python (ie: the and/or operators)
This method searches for the first group of word characters (ie: [a-zA-Z0-9_]), returning the first matched group or None in case of failure.
def test(str):
m = re.search(r'(\w+)', str)
if m:
return m.group(1)
return None
The…
NullUserException
- 77,975
- 25
- 199
- 226
6
votes
2 answers
Reason for allowing Special Characters in Python Attributes
I somewhat accidentally discovered that you can set 'illegal' attributes to an object using setattr. By illegal, I mean attributes with names that can't be retrieve using the __getattr__ interface with traditional . operator references. They can…
Keozon
- 836
- 9
- 24
6
votes
1 answer
Shouldn't using FieldInfo.SetValue to set a ValueType to null fail?
(related to PropertyInfo SetValue and nulls)
If I have public class Thing { public int X; }, a Thing o, and a FieldInfo fi that points to the X field, why is it legal to call fi.SetValue(o, null)? The runtime sets the field X to zero, i.e.…
Sebastian Good
- 6,091
- 2
- 28
- 54
4
votes
1 answer
Default function values in multi-layer architecture
Wondering the best way to set defaults in a multi-layer application structure. Specifically, if a certain work flow requires a nested set of function calls, is the default specified on all the functions, or just on the top level function and passed…
Clay Wardell
- 12,466
- 11
- 39
- 56
3
votes
2 answers
Make ++o++ complain for types with user defined pre- and postfix increment operators
I'm looking for a way to prevent ++x++ from working for types with user defined prefix and postfix increment operators.
For builtin types the result type of the postfix operator is not an lvalue but a prvalue expression and the compilers complain…
jesses
- 352
- 9
2
votes
1 answer
Oddly Ruby behavior
I need to check if a variable is an array, and if not convert it into one before proceed with further processing. So, my code looks like this:
class Test < Struct.new(:args)
def eval
p "1. #{args}"
args = (args.instance_of?…
cheng81
- 2,374
- 2
- 19
- 18
2
votes
1 answer
Collection initializers inside object initializers with default values
I just stumbled upon the following issue:
class Settings
{
// Let's set some default value: { 1 }
public ICollection AllowedIds = new List() { 1 };
}
static void Main(string[] args)
{
var s = new Settings
{
…
Heinzi
- 151,145
- 51
- 326
- 481
2
votes
1 answer
Returning `self` at ActiveRecord class method loses indirect reference
When defining a class method at an ActiveRecord, if I return self, the indirect reference is lost.
I'm not sure if I am using the right vocabulary, as I am just learning Ruby on Rails, so here is an example:
class User < ActiveRecord::Base
…
Thiago Negri
- 4,965
- 2
- 25
- 38
1
vote
1 answer
Should I give the backing beans another name than the class?
Should I give the backing beans a new name in the @Named annotation, or should I use the same as the class for readability? Are there any guidelines on when to not or when to do this?
I have a backing bean that provides a dropdown component with…
user626912
- 2,406
- 3
- 21
- 32
0
votes
0 answers
Mutable vs immutable object behavior in python functions
the two pieces of code below produce different outputs.
def f1(x= []):
x.append(1)
return x
print(f1())
print(f1())
Output is:
[1]
[1, 1]
And for the second function:
def f2(x=0):
x+=1
return x
print(f2())
print(f2())
Output…
ste_kwr
- 348
- 1
- 2
- 12
0
votes
0 answers
Prevent conversion to factor when number of columns in a data.frame can be reduced to one
I have a procedure that can extract items from a data frame based on a list of conditions on the columns (see Extracting items from an R data frame using criteria given as a (column_name = value) list):
Here are the data frame and condition list:
>…
bli
- 5,749
- 2
- 34
- 76
0
votes
3 answers
Recurring dates on dates that do not exist
When giving the option for something to reoccur every certain amount of time how should I treat times that don't reoccur on every interval?
For example what should happen to birthday reminders for February 29th? Or if I have a monthly appointment on…
Motti
- 99,411
- 44
- 178
- 249
-2
votes
1 answer
What are the implications of using mutable types as default arguments in Python?
Possible Duplicates:
Why the “mutable default argument fix” syntax is so ugly, asks python newbie
least astonishment in python: the mutable default argument
Here is an example.
def list_as_default(arg = []):
pass
Eric Schoonover
- 44,080
- 43
- 148
- 200