scope

short form for string.format(…,**locals())

一世执手 提交于 2019-12-08 18:10:35
问题 I usually use the following pattern (as mentioned in this question): a=1 s= "{a}".format(**locals()) I think it's a great way to write easily readable code. Sometimes it's useful to "chain" string formats, in order to "modularize" the creation of complex strings: a="1" b="2" c="{a}+{b}".format(**locals()) d="{c} is a sum".format(**locals()) #d=="1+2 is a sum" Pretty soon, the code is pestered with X.format(**locals()) . To solve this problem, I tried to create a lambda: f= lambda x: x.format(

Accessing the outer scope in Python 2.6

吃可爱长大的小学妹 提交于 2019-12-08 17:43:21
问题 Say, I have some scope with variables, and a function called in this scope wants to change some immutable variables: def outer(): s = 'qwerty' n = 123 modify() def modify(): s = 'abcd' n = 456 Is it possible somehow to access the outer scope? Something like nonlocal variables from Py3k. Sure I can do s,n = modify(s,n) in this case, but what if I need some generic 'injection' which executes there and must be able to reassign to arbitrary variables? I have performance in mind, so, if possible,

Rails 3 Routing - How to use scope to create admin prefix

独自空忆成欢 提交于 2019-12-08 17:11:33
问题 I'm using this Rails Guide to create a scope in order to create an "/admin" prefix for some controllers. So I have a controller named Pages, I want to access it via "/admin/pages". scope "/admin" do resources :pages end That works great, but it is still accessible via "/pages" ... How do I prevent that? (I'm using Rails 3) Here's my routes file: devise_for :users scope "/admin" do resources :pages resources :contents end root :to => "index#index" match ':controller(/:action(/:id(.:format)))'

Rails ActiveRecord Scope that is the “opposite” of another scope, or is users that “lack” a property

荒凉一梦 提交于 2019-12-08 17:09:44
问题 I have a model for user.rb, in which I define a scope for admins , which is users that have the role of admin through a permissions table. has_many :permissions has_many :roles, :through => :permissions The scope works like this: scope :admins, joins(:permissions).merge(Permission.admin_permissions) I'd also like to make a scope called non-admins or something like that, which is all users that do NOT have the admin role. What's the easiest way to do this? 回答1: An easy way, which would not be

Facebook returns undefined for email and birthday

我的未来我决定 提交于 2019-12-08 16:15:05
问题 I am trying to use the Facebook API that provides first_name , last_name , gender , email , birthday for signup on my website. I used the Graph API and generated an access token with necessary scope. I tried on my personal account in which I created the app and it returned the fields properly, but for FB test user and friend's account, it returns email and birthday as "undefined". In graph.facebook.com it shows the following error for a friend's account. Facebook returns email id for my

Scope in coffeescript classes

岁酱吖の 提交于 2019-12-08 15:59:14
问题 I would like to nest a number of functions inside a class property as shown below. Unfortunately, they won't get access to the main scope of the class. Can I solve this without passing each nested function a reference to this ? class myClass constructor: -> @errors = [] doSomething: -> @errors.push "I work as expected" functions: doStuff: -> @errors.push "I cant access @errors" # => TypeError: Cannot call method 'push' of undefined ugly: (context) -> context.errors.push "It works, but I am

Declaring two global variables of same name in C

泄露秘密 提交于 2019-12-08 15:46:33
问题 I have declared two global variables of same name in C. It should give error as we cannot declare same name variables in same storage class. I have checked it in C++ — it gives a compile time error, but not in C. Why? Following is the code: int a; int a = 25; int main() { return 0; } Check it out at : Code Written at Ideone I think this probably is the reason Declaration and Definition in C But this is not the case in C++. I think in C++, whether the variable is declared at global scope or

objective c xcode 4.0.2: subclass can't access superclass variables “was not declared in this scope”

二次信任 提交于 2019-12-08 14:46:32
I have several classes that are subclasses of one Layer class.. for some reason one of the subclasses acts differently. this is a stripped down version: @interface Layer: CCLayer { GameScene* _scene; CCNode* _layerNode; } @end #import "Layer.h" @interface UILayer: Layer { } @end @implementation UILayer -(void) doStuff { [_layerNode addChild:[CCNode node]]; <---gives compile error: "_layerNode was not declared in this scope" [_scene playSound];<------gives compile error: "_scene was not declared in this scope" } @end I think that gets the basic idea across. I can fix this by doing [[_self

In Haskell, what is the scope of a where clause when dealing with guards?

二次信任 提交于 2019-12-08 14:43:23
问题 I know they do not hold across pattern matches (i.e. you need to rewrite the 'where' clause for each pattern), but how does the scoping work for guards? e.g. Does this work? myFunction x1 x2 | x1 > x2 = addOne x1 | x1 < x2 = addOne x2 | otherwise = x1 where addOne = (1+) Or should it be this? myFunction x1 x2 | x1 > x2 = addOne x1 where addOne = (1+) | x1 < x2 = addOne x2 where addOne = (1+) | otherwise = x1 回答1: The first one is the correct one. I would suggest you to have a look at the let

What is the scope of a namespace alias in C++?

瘦欲@ 提交于 2019-12-08 14:37:59
问题 Does a C++ namespace alias defined inside a function definition have a block, function, file, or other scope (duration of validity)? 回答1: It's a block duration of validity. E.g If you define a namespace alias as below, the namespace alias abc would be invalid outside {...} block. { namespace abc = xyz; abc::test t; //valid } abc::test t; //invalid 回答2: The scope is the declarative region in which the alias is defined. 回答3: It would have the scope of the block in which it was defined - likely