default

How can I return a default value for an attribute? [duplicate]

拈花ヽ惹草 提交于 2019-11-30 06:06:13
This question already has an answer here: A get() like method for checking for Python attributes 3 answers I have an object myobject , which might return None . If it returns None , it won't return an attribute id : a = myobject.id So when myobject is None , the stament above results in a AttributeError : AttributeError: 'NoneType' object has no attribute 'id' If myobject is None, then I want a to be equal to None . How do I avoid this exception in one line statement, such as: a = default(myobject.id, None) Inbar Rose You should use the getattr wrapper instead of directly retrieving the value

Namespace agnostic XPath query with element content

a 夏天 提交于 2019-11-30 05:10:12
问题 The namespace agnostic syntax I've seen around is confusing me. Say I have: <root> <parent attribute="A">A<child>A</child></parent> <parent attribute="B">B<child>B</child></parent> </root> So far I see how: /root/parent/child/text() translates to: /*[local-name()='root']/*[local-name()='parent']/*[local-name()='child']/text() but i'm struggling with things like this: /root/parent[@attribute="A"]/child/text() or: /root/parent[text()="B"]/child/text() or: /root/parent[1]/child/text() How do

Rails: how to use scope with params and use route with default value of params

社会主义新天地 提交于 2019-11-30 03:53:02
I have such lines in routes.rb: scope "/:subdomain/" do resource :order, :only => [:new, :create, :show, :update, :edit, :destroy] do get :cancel, :on => :member put :counter, :on => :member end end And for example, this is typical url: http://mydomain.com/some_subdomain/order/new . This url is mapped to action new of orders controller with params[:subdomain] = "some_subdomain". I want to use named route new_order_url(:subdomain => "some_subdomain"). But I want to map http://mydomain.com/order/new to orders controller, action new and params[:subdomain] = "default". And I want to use named

Why when I insert a DateTime null I have “0001-01-01” in SQL Server?

天大地大妈咪最大 提交于 2019-11-30 03:42:25
问题 I try to insert the value null (DateTime) in my database for a field typed 'date' but I always get a '0001-01-01' . I don't understand, this field "allow nulls" and I don't know why I have this default value. I'm using C# asp .net with MVC (Entity Framework), this is my code : Budget_Synthesis newBS = new Budget_Synthesis { Budget_Code = newBudgetCode, Last_Modified_Date = null }; db.Budget_Synthesis.AddObject(newBS); Last_Modified_Date is typed System.DateTime? so I don't know why they

Why does Hash.new({}) hide hash members? [duplicate]

∥☆過路亽.° 提交于 2019-11-30 01:40:26
问题 This question already has answers here : Strange, unexpected behavior (disappearing/changing values) when using Hash default value, e.g. Hash.new([]) (4 answers) Closed 4 years ago . Ok, so I wanted to create a hash which has an empty hash as the default value. A bit weird, I know, but let's say that I thought it might be useful. So here's what I did: >> a = Hash.new({}) => {} >> a[:a][:b] = 5 => 5 >> a => {} >> a[:a] => {:b=>5} >> a.keys => [] >> a.size => 0 >> a[:a].size => 1 In other words

Overload a method or use default values? c++

点点圈 提交于 2019-11-30 00:46:12
问题 I'm still relatively new to C++ and I can't seem to figure out the difference in the following two ways of coding a function that may take one parameter or maybe two or three or more. Anyway, here's my point function overload: int aClass::doSomething(int required) { //DO SOMETHING } int aClass::doSomething(int required, int optional) { //DO SOMETHING } how is this different to, default value: int aClass::doSomething(int required, int optional = 0) { //DO SOMETHING } I know in different

default value for a static property

半腔热情 提交于 2019-11-30 00:05:39
问题 I like c#, but why can I do : public static bool Initialized { private set; get; } or this : public static bool Initialized = false; but not a mix of both in one line ? I just need to set access level to my variable (private set), and I need it set at false on startup. I wouldn't like to make that boring private _Initialized variable, which would be returned by the getter of the public Initialized var. I like my code to be beautiful. (NB: my variable is static, it can't be initialized in the

Prevent default behavior in text input while pressing arrow up

不打扰是莪最后的温柔 提交于 2019-11-29 22:56:28
I’m working with basic HTML <input type="text"/> text field with a numeric value. I’m adding JavaScript event keyup to see when user presses arrow up key ( e.which == 38 ) – then I increment the numeric value. The code works well, but there’s one thing that bugs me. Both Safari/Mac and Firefox/Mac move cursor at the very beginning when I’m pressing the arrow up key. This is a default behavior for every <input type="text"/> text field as far as I know and it makes sense. But this creates not a very aesthetic effect of cursor jumping back and forward (after value was altered). The jump at the

What is default color for text in textview?

99封情书 提交于 2019-11-29 22:00:23
I set the color to red , and after that I want to set the color again back to default, but I do not know what is default color, does anyone knows ? You can save old color and then use it to restore the original value. Here is an example: ColorStateList oldColors = textView.getTextColors(); //save original colors textView.setTextColor(Color.RED); .... textView.setTextColor(oldColors);//restore original colors But in general default TextView text color is determined from current Theme applied to your Activity . Actually the color TextView is: android:textColor="@android:color/tab_indicator_text"

c++ publicly inherited class member cannot be used as default argument

与世无争的帅哥 提交于 2019-11-29 15:39:58
A schematic of my problem... class A { public: // etc. protected: uint num; }; class B : public A { public: void foo(uint x = num); //bad }; gives this error: error: invalid use of non-static data member ‘A::num’ error: from this location Why does this happen, and what can I do to work around this? I suspect this happens (based on the complaint about non-staticness) because there is no this pointer for it to use to know which instance of B it should get num from. The Microsoft compiler (at least) allows you to specify an expression, but not a non-static member. From MSDN : The expressions used