default-value

What is the scope of a defaulted parameter in Python?

孤者浪人 提交于 2019-11-28 17:54:16
When you define a function in Python with an array parameter, what is the scope of that parameter? This example is taken from the Python tutorial: def f(a, L=[]): L.append(a) return L print f(1) print f(2) print f(3) Prints: [1] [1, 2] [1, 2, 3] I'm not quite sure if I understand what's happening here. Does this mean that the scope of the array is outside of the function? Why does the array remember its values from call to call? Coming from other languages, I would expect this behavior only if the variable was static. Otherwise it seems it should be reset each time. And actually, when I tried

Default value on JSP custom-tag attribute

筅森魡賤 提交于 2019-11-28 17:44:53
When defining an attribute for a custom JSP tag, is it possible to specify a default value? The attribute directive doesn't have a default value attribute. Currently I'm making do with: <%@ attribute name="myAttr" required="false" type="java.lang.String" %> <c:if test="${empty myAttr}" > <c:set var="myAttr" value="defaultValue" /> </c:if> Is there a better way? G. Demecki There is a better way: <c:set var="title" value="${(empty title) ? 'Default title' : title}" /> No need for custom tag in Java nor tld. Just plain JSP EL and conditional operator. In my opinion it is shorter and cleaner than

Is there a better PHP way for getting default value by key from array (dictionary)?

狂风中的少年 提交于 2019-11-28 17:25:55
In Python one can do: foo = {} assert foo.get('bar', 'baz') == 'baz' In PHP one can go for a trinary operator as in: $foo = array(); assert( (isset($foo['bar'])) ? $foo['bar'] : 'baz' == 'baz'); I am looking for a golf version. Can I do it shorter/better in PHP? I just came up with this little helper function: function get(&$var, $default=null) { return isset($var) ? $var : $default; } Not only does this work for dictionaries, but for all kind of variables: $test = array('foo'=>'bar'); get($test['foo'],'nope'); // bar get($test['baz'],'nope'); // nope get($test['spam']['eggs'],'nope'); // nope

Javascript Get Element by Id and set the value

孤街浪徒 提交于 2019-11-28 17:22:16
I have a javascript function to which I pass a parameter. The parameter represents the id of an element (a hidden field) in my web page. I want to change the value of this element. function myFunc(variable){ var s= document.getElementById(variable); s.value = 'New value' } When I do this, I get an error that the value cannot be set because the object is null. But I know the object is not null because I see it in the html code generated by the browser. Anyways, I tried the following code to debug function myFunc(variable){ var x = variable; var y = 'This-is-the-real-id' alert(x + ', ' + y) var

Display a default DataTemplate in a ContentControl when its content is null or empty?

▼魔方 西西 提交于 2019-11-28 17:12:16
I would think this is possible, but the obvious way isn't working. Currently, I'm doing this: <ContentControl Content="{Binding HurfView.EditedPart}"> <ContentControl.Resources> <Style TargetType="ContentControl" x:Key="emptytemplate"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Content}" Value="{x:Null}"> <Setter Property="ContentControl.Template"> <Setter.Value> <ControlTemplate> <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <TextBlock>EMPTY!</TextBlock> </Grid> </ControlTemplate> </Setter.Value> </Setter> <

SQL Column definition : default value and not null redundant?

佐手、 提交于 2019-11-28 16:47:43
I've seen many times the following syntax which defines a column in a create/alter DDL statement: ALTER TABLE tbl ADD COLUMN col VARCHAR(20) NOT NULL DEFAULT "MyDefault" The question is: since a default value is specified, is it necessary to also specify that the column should not accept NULLs ? In other words, doesn't DEFAULT render NOT NULL redundant ? DEFAULT is the value that will be inserted in the absence of an explicit value in an insert / update statement. Lets assume, your DDL did not have the NOT NULL constraint: ALTER TABLE tbl ADD COLUMN col VARCHAR(20) DEFAULT "MyDefault" Then you

Best way to give a variable a default value (simulate Perl ||, ||= )

£可爱£侵袭症+ 提交于 2019-11-28 15:55:52
I love doing this sort of thing in Perl: $foo = $bar || $baz to assign $baz to $foo if $bar is empty or undefined. You also have $foo ||= $bletch which will only assign $bletch to $foo if $foo is not defined or empty. The ternary operator in this situation is tedious and tiresome. Surely there's a simple, elegant method available in PHP? Or is the only answer a custom function that uses isset()? PHP 5.3 has a shorthand ?: operator: $foo = $bar ?: $baz; Which assigns $bar if it's not an empty value (I don't know how this would be different in PHP from Perl), otherwise $baz , and is the same as

How to create default value for function argument in Clojure

為{幸葍}努か 提交于 2019-11-28 15:26:34
I come with this: (defn string->integer [str & [base]] (Integer/parseInt str (if (nil? base) 10 base))) (string->integer "10") (string->integer "FF" 16) But it must be a better way to do this. A function can have multiple signatures if the signatures differ in arity. You can use that to supply default values. (defn string->integer ([s] (string->integer s 10)) ([s base] (Integer/parseInt s base))) Note that assuming false and nil are both considered non-values, (if (nil? base) 10 base) could be shortened to (if base base 10) , or further to (or base 10) . You can also destructure rest arguments

AngularJS directive with default options

混江龙づ霸主 提交于 2019-11-28 15:26:21
I'm just starting with angularjs, and am working on converting a few old JQuery plugins to Angular directives. I'd like to define a set of default options for my (element) directive, which can be overridden by specifying the option value in an attribute. I've had a look around for the way others have done this, and in the angular-ui library the ui.bootstrap.pagination seems to do something similar. First all default options are defined in a constant object: .constant('paginationConfig', { itemsPerPage: 10, boundaryLinks: false, ... }) Then a getAttributeValue utility function is attached to

HTML/PHP - default input value

六眼飞鱼酱① 提交于 2019-11-28 13:30:02
I have a post php form and a set of inputs: Your Name Your Last Name My Name Every input looks the same, only the names change: <input type="text" name="your_name" value="<?php echo get_option('your_name'); ?>" /> How to set default values when value= is not available? [edit] Ok, so, normally I'd do something like: <input type="text" name="your_name" value="Mike" /> But in this case I have a PHP script that grabs inputs data and displays it using value="<?php echo get_option('your_name'); ?>" . So I have no idea how to force my form to display "Mike" in my input. You need to check the return