scope

How to access to anonymous namespace variable if the same variable exists in global

我们两清 提交于 2019-12-23 10:09:24
问题 Let's imagine situation: #include <iostream> int d =34; namespace { int d =45; } int main() { std::cout << ::d ; return 0; } Here the output is 34, because :: means global namespace. But If I comment 3rd line the output is 45, which is strange . If I use std::cout << d ; - I get error s.cxx:12:15: error: reference to ‘d’ is ambiguous How can I access unnamed_namespace::d in this scenario? PS: I've read that unnamed namespace is used for static global variables aka visible only in file scope

Prefixing variables names to indicate their respective scope or origin?

我是研究僧i 提交于 2019-12-23 10:07:00
问题 In the companies I've been working, I've seen a lot the use of prefixes to indicate the scope or the origin of variables, for example m for classes members, i for methods intern variables and a (or p ) for methods parameters: public class User { private String mUserName; public String setUserName(final String aUserName) { final String iUserName = "Mr " + aUserName; mUserName = iUserName; } } What do you think about it? Is it recommended (or precisely not)? I found it quite ugly in a first

Weird lambda behaviour in loops [duplicate]

浪子不回头ぞ 提交于 2019-12-23 09:30:08
问题 This question already has answers here : Local variables in nested functions (3 answers) Closed 6 years ago . I stumbled upon a behaviour in python that I have a hard time understanding. This is the proof-of-concept code: from functools import partial if __name__ == '__main__': sequence = ['foo', 'bar', 'spam'] loop_one = lambda seq: [lambda: el for el in seq] no_op = lambda x: x loop_two = lambda seq: [partial(no_op, el) for el in seq] for func in (loop_one, loop_two): print [f() for f in

Supporting lexical-scope ScriptBlock parameters (e.g. Where-Object)

风流意气都作罢 提交于 2019-12-23 09:22:03
问题 Consider the following arbitrary function and test cases: Function Foo-MyBar { Param( [Parameter(Mandatory=$false)] [ScriptBlock] $Filter ) if (!$Filter) { $Filter = { $true } } #$Filter = $Filter.GetNewClosure() Get-ChildItem "$env:SYSTEMROOT" | Where-Object $Filter } ################################## $private:pattern = 'T*' Get-Help Foo-MyBar -Detailed Write-Host "`n`nUnfiltered..." Foo-MyBar Write-Host "`n`nTest 1:. Piped through Where-Object..." Foo-MyBar | Where-Object { $_.Name -ilike

How can “NameError: free variable 'var' referenced before assignment in enclosing scope” occur in real code?

大城市里の小女人 提交于 2019-12-23 09:06:07
问题 While I was hanging out in the Python chatroom, someone dropped in and reported the following exception: NameError: free variable 'var' referenced before assignment in enclosing scope I'd never seen that error message before, and the user provided only a small code fragment that couldn't have caused the error by itself, so off I went googling for information, and ... there doesn't seem to be much. While I was searching, the user reported their problem solved as a "whitespace issue", and then

Python: Regarding variable scope. Why don't I need to pass x to Y?

巧了我就是萌 提交于 2019-12-23 08:52:56
问题 Consider the following code, why don't I need to pass x to Y? class X: def __init__(self): self.a = 1 self.b = 2 self.c = 3 class Y: def A(self): print(x.a,x.b,x.c) x = X() y = Y() y.A() Thank you to the top answers, they really helped me see what was the problem, namely misunderstanding regarding variable scope. I wish I could choose both as correct answer as they are enlightening in their own way. 回答1: From The Python Tutorial: Although scopes are determined statically, they are used

Scope of variables (Hoisting) in Javascript

流过昼夜 提交于 2019-12-23 08:52:50
问题 One of my friends was taking an online quiz and he asked me this question which I could not answer. var global = false; function test() { global = true; return false; function global() {} } console.log(global); // says false (As expected) test(); console.log(global); // says false (Unexpected: should be true) If we assume that functions are hoisted at the top along with var variables, let's try this one. var foo = 1; function bar() { return foo; foo = 10; function foo() {} var foo = 11; } bar

Python: Regarding variable scope. Why don't I need to pass x to Y?

。_饼干妹妹 提交于 2019-12-23 08:52:20
问题 Consider the following code, why don't I need to pass x to Y? class X: def __init__(self): self.a = 1 self.b = 2 self.c = 3 class Y: def A(self): print(x.a,x.b,x.c) x = X() y = Y() y.A() Thank you to the top answers, they really helped me see what was the problem, namely misunderstanding regarding variable scope. I wish I could choose both as correct answer as they are enlightening in their own way. 回答1: From The Python Tutorial: Although scopes are determined statically, they are used

Matlab function handle workspace shenanigans

佐手、 提交于 2019-12-23 07:39:29
问题 In short : is there an elegant way to restrict the scope of anonymous functions, or is Matlab broken in this example? I have a function that creates a function handle to be used in a pipe network solver. It takes as input a Network state which includes information about the pipes and their connections (or edges and vertices if you must), constructs a large string which will return a large matrix when in function form and "evals" that string to create the handle. function [Jv,...] =

Does scoping in julia 1.0.0 with for-loops make sense to beginners? [closed]

£可爱£侵袭症+ 提交于 2019-12-23 07:11:13
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . In julia 1.0.0, I get the following for-loop scoping behavior: julia> counts = 0 0 julia> for i in 1:10 counts += 1 end ERROR: UndefVarError: counts not defined I found the solution was to make the counts variable global inside the for loop. julia> for i in 1:10 global counts +