假设我正在使用irb,并输入a = 5.如何删除定义,a以便键入a返回NameError?
一些背景:后来我想这样做:
context = Proc.new{}.binding
context.eval 'a = 5'
context.eval 'undef a' # though this doesn't work.
Run Code Online (Sandbox Code Playgroud) 关于使用以下模式是否有任何缺点,警告或不良做法警告?
def buildString(user, name = 'john', age=22):
userId = user.getUserId()
return "Name: {name}, age: {age}, userid:{userId}".format(**locals())
Run Code Online (Sandbox Code Playgroud)
我有一个非常重复的字符串生成代码来编写并且很想使用它,但是关于使用的东西locals()让我感到不舒服.这有意外行为的危险吗?
编辑:上下文
我发现自己经常写下这样的东西:
"{name} {age} {userId} {etc}...".format(name=name, age=age, userId=userId, etc=etc)
Run Code Online (Sandbox Code Playgroud) 我试图从一个表中获取BrandID的值并将其添加到另一个表中.但我无法让它发挥作用.有谁知道怎么做对吗?
CREATE PROCEDURE AddBrand
AS
DECLARE
@BrandName nvarchar(50),
@CategoryID int,
@BrandID int
SELECT @BrandID = BrandID FROM tblBrand
WHERE BrandName = @BrandName
INSERT INTO tblBrandinCategory (CategoryID, BrandID)
VALUES (@CategoryID, @BrandID)
RETURN
Run Code Online (Sandbox Code Playgroud) 我看了这个视频.为什么a = a评估nil是否a未定义?
a = a # => nil
b = c = q = c # => nil
Run Code Online (Sandbox Code Playgroud) 我有以下Ruby代码:
local_var = "Hello"
def hello
puts local_var
end
hello
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
local_variables.rb:4:in 'hello': undefined local variable or method 'local_var'
for main:Object (NameError) from local_variables.rb:7:in '<main>'
Run Code Online (Sandbox Code Playgroud)
我一直认为局部变量不能从块外部,函数,闭包等访问.
但是现在我在文件中定义了局部变量,并尝试从INSIDE函数获取对同一文件的访问权限 .
我的理解有什么问题?
如何在try/except块public中创建变量?
import urllib.request
try:
url = "http://www.google.com"
page = urllib.request.urlopen(url)
text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError, NameError):
print("Unable to process your request dude!!")
print(text)
Run Code Online (Sandbox Code Playgroud)
此代码返回错误__CODE__.
如何在try/except块之外使变量文本可用?
#include <stdio.h>
int foo1(void)
{
int p;
p = 99;
return p;
}
char *foo2(void)
{
char buffer[] = "test_123";
return buffer;
}
int *foo3(void)
{
int t[3] = {1,2,3};
return t;
}
int main(void)
{
int *p;
char *s;
printf("foo1: %d\n", foo1());
printf("foo2: %s\n", foo2());
printf("foo3: %d, %d, %d\n", p[0], p[1], p[2]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译它时gcc -ansi -pedantic -W -Wall,编译器会发出foo2()和foo3()的警告消息:
warning: function returns address of local variable
Run Code Online (Sandbox Code Playgroud)
我认为不允许返回局部变量,但foo1()工作正常,似乎返回指向本地对象的指针和对象本身之间存在巨大差异.
任何人都能对这个问题有所了解吗?提前致谢!
我想知道是否有使用最终局部变量的可用性.当继承进入图像时,无论如何都不会覆盖变量.例如,如下的简单代码
public static void main(String args[]) {
final String data = "Hello World!";
System.out.println(data);
}
Run Code Online (Sandbox Code Playgroud)
这个例子非常简单,可能不是相关的代码,但问题更通用.我看过很多代码(都包含在main函数中,它们都有最终的局部变量)是否有任何可用性来声明局部变量作为最终的其他而不是他们不能在同一个功能本身编辑?
我试图在Python中访问函数外部的本地函数变量.所以,例如,
bye = ''
def hi():
global bye
something
something
bye = 5
sigh = 10
hi()
print bye
Run Code Online (Sandbox Code Playgroud)
以上工作正常.由于我想知道我是否可以bye在hi()不使用的情况下访问外部global bye,我试过:
def hi():
something
something
bye = 5
sigh = 10
return
hi()
x = hi()
print x.bye
Run Code Online (Sandbox Code Playgroud)
以上给出AttributeError: 'NoneType' object has no attribute 'bye'.
然后,我试过:
def hi():
something
something
bye = 5
sigh = 10
return bye
hi()
x = hi()
print x.bye
Run Code Online (Sandbox Code Playgroud)
这次它甚至没有出错.
那么,有没有办法在不使用全局变量的情况下访问函数(bye)之外的本地函数变量()hi()而不打印变量sigh?(问题编辑后包括 …
在阅读并讨论Java 10s新保留类型名称var
(JEP 286:局部变量类型推断)之后,讨论中出现了一个问题.
与文字一起使用时,如:
var number = 42;
Run Code Online (Sandbox Code Playgroud)
是number现在的int还是一个Integer?如果您只是将它与比较运算符一起使用或作为参数使用它通常无关紧要,这要归功于自动装箱和开箱.但由于Integer成员职能,它可能很重要.
那么哪个类型是由原var语int或类创建的Integer?
local-variables ×10
python ×3
ruby ×3
java ×2
c ×1
final ×1
java-10 ×1
locals ×1
pointers ×1
python-3.x ×1
scope ×1
sql-server ×1
try-except ×1