scope

Function variable scope in python

徘徊边缘 提交于 2021-02-05 09:38:26
问题 let's say we have two functions: def ftpConnect(): ftp = FTP('server') ftp.login() ftp.cwd('/path') def getFileList(): ftpConnect() files = ftp.nlst() print(files) If I call the getFileList() function it won't work because it doesn't know the ftp var. I know that if I declare the ftp variable inside ftpConnect() function as global it will work, but I was wondering if there is a better / more elegant way of doing it. 回答1: In my opinion, the most elegant solution would be to make a FTP-class,

I don't know the scope of var

半城伤御伤魂 提交于 2021-02-04 18:31:13
问题 I want to make a table with vanilla js. But I got a different result depending on where I declare the var. var body = document.body; var table = document.createElement('table'); var tr = document.createElement('tr'); var td = document.createElement('td'); body.appendChild(table); for (var i = 0; i < 3; i +=1) { table.appendChild(tr); for (var j = 0; j < 3; j+=1) { tr.appendChild(td); td.textContent = 'td'; } } I wanted to make 3*3 table. But it made 1*1 table. var body = document.body; var

I don't know the scope of var

被刻印的时光 ゝ 提交于 2021-02-04 18:31:08
问题 I want to make a table with vanilla js. But I got a different result depending on where I declare the var. var body = document.body; var table = document.createElement('table'); var tr = document.createElement('tr'); var td = document.createElement('td'); body.appendChild(table); for (var i = 0; i < 3; i +=1) { table.appendChild(tr); for (var j = 0; j < 3; j+=1) { tr.appendChild(td); td.textContent = 'td'; } } I wanted to make 3*3 table. But it made 1*1 table. var body = document.body; var

Why does const work in some for-loops in JavaScript?

核能气质少年 提交于 2021-02-04 10:08:10
问题 I do know why const doesn't work in for-loops. We need to create a new scope and copy over a value into that. So this won't fly. for(const i = 0; i < 5; i++) console.log(i); Whereas this will. for(let i = 0; i < 5; i++) console.log(i); However, I noticed that both of them work when looping though the properties of an object like this. for(let property in thingy) console.log(property); for(const property in thingy) console.log(property); I'm not sure why. 回答1: for (const property in object)

Why does const work in some for-loops in JavaScript?

南楼画角 提交于 2021-02-04 10:04:03
问题 I do know why const doesn't work in for-loops. We need to create a new scope and copy over a value into that. So this won't fly. for(const i = 0; i < 5; i++) console.log(i); Whereas this will. for(let i = 0; i < 5; i++) console.log(i); However, I noticed that both of them work when looping though the properties of an object like this. for(let property in thingy) console.log(property); for(const property in thingy) console.log(property); I'm not sure why. 回答1: for (const property in object)

Weird scoping behavior in python

强颜欢笑 提交于 2021-02-04 09:25:07
问题 Consider the following snippet of python code: x = 1 class Foo: x = 2 def foo(): x = 3 class Foo: print(x) # prints 3 Foo.foo() As expected, this prints 3. But, if we add a single line to the above snippet, the behavior changes: x = 1 class Foo: x = 2 def foo(): x = 3 class Foo: x += 10 print(x) # prints 11 Foo.foo() And, if we switch the order of the two lines in the above example, the result changes yet again: x = 1 class Foo: x = 2 def foo(): x = 3 class Foo: print(x) # prints 1 x += 10

javascript custom scope binding function

安稳与你 提交于 2021-01-29 22:02:54
问题 I am reading this article - http://www.robertsosinski.com/2009/04/28/binding-scope-in-javascript/ - where a custom bind function is made. Function.prototype.bind = function(scope) { var _function = this; return function() { return _function.apply(scope, arguments); } } alice = { name: "alice" } eve = { talk: function(greeting) { console.log(greeting + ", my name is " + this.name); }.bind(alice) // <- bound to "alice" } eve.talk("hello"); // hello, my name is alice My question is this line in

locals() built-in method in Python3 returns the variable in the global namespace

时光怂恿深爱的人放手 提交于 2021-01-29 16:47:05
问题 name = "Lenin Mishra" def home(): name = "Sonu" print(name) home() print(locals()) When I run the above code, Python returns me a dictionary containing the variable name, which has a value of Lenin Mishra . {'__name__': '__main__', '__doc__': '\nExperimenting with scope\n', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10323b400>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users

What is side effect in C?

落花浮王杯 提交于 2021-01-29 14:11:59
问题 Wikipedia says that: In computer science, an operation, function or expression is said to have a side effect if it modifies some state variable value(s) outside its local environment, that is to say has an observable effect besides returning a value (the main effect) to the invoker of the operation. But how can we access a variable outside its local environment, can anyone explain this situation, side effect, main effect and sequence point comprehensibly? 回答1: A function is (should be) a

How to prevent firebase db access from chrome console or other methods

佐手、 提交于 2021-01-29 11:03:32
问题 So I have a single page frontend only app. Right now I have something like this // db.js import firebase from "firebase/app" import "firebase/firestore"; var firebaseConfig = { ... }; export const db = firebase .initializeApp(firebaseConfig) .firestore(); in main.js I was experimenting with putting the db instance in the global window scope just to see if I could go to the chrome web console and access it to submit a doc and indeed I can // main.js import { db } from './db' window.db = db;