标签: scope

在用c ++初始化对象之前声明一个对象

是否可以在不实例化的情况下在c ++中声明变量?我想做这样的事情:

Animal a;
if( happyDay() ) 
    a( "puppies" ); //constructor call
else
    a( "toads" );
Run Code Online (Sandbox Code Playgroud)

基本上,我只是想声明条件的外部,以便它获得正确的范围.

有没有办法在不使用指针和a在堆上分配的情况下执行此操作?也许引用聪明的东西?

c++ scope declaration instantiation

47
推荐指数
7
解决办法
4万
查看次数

Rails:为什么with_exclusive_scope受到保护?关于如何使用它的任何好的做法?

给定一个带有default_scope的模型来过滤所有过时的条目:

# == Schema Information
#
#  id          :integer(4)      not null, primary key
#  user_id     :integer(4)      not null, primary key
#  end_date    :datetime        

class Ticket < ActiveRecord::Base
  belongs_to :user
  default_scope :conditions => "tickets.end_date > NOW()"
end
Run Code Online (Sandbox Code Playgroud)

现在我想得到任何票.在这种情况下,with_exclusive_scope是要走的路,但这种方法是否受到保护?只有这个有效:

 Ticket.send(:with_exclusive_scope) { find(:all) }
Run Code Online (Sandbox Code Playgroud)

有点黑客,不是吗?那么正确的使用方法是什么?特别是在处理协会时,情况变得更糟(假设用户有很多票):

 Ticket.send(:with_exclusive_scope) { user.tickets.find(:all) }
Run Code Online (Sandbox Code Playgroud)

这是如此丑陋! - 不能成为轨道!?

scope ruby-on-rails

47
推荐指数
3
解决办法
3万
查看次数

为什么我们不能在if语句中定义变量?

也许这个问题之前已经得到了回答,但是这个词if经常出现,很难找到它.

这个例子没有意义(表达式总是正确的),但它说明了我的问题.

为什么此代码有效:

StringBuilder sb;
if ((sb = new StringBuilder("test")) != null) {
    Console.WriteLine(sb);
}
Run Code Online (Sandbox Code Playgroud)

但是这段代码不是:

if ((StringBuilder sb = new StringBuilder("test")) != null) {
    Console.WriteLine(sb);
}
Run Code Online (Sandbox Code Playgroud)

我发现了一个关于while声明的类似问题.那里接受的答案说,在一个while声明中,它意味着变量将在每个循环中定义.但是对于我的if陈述例子,情况并非如此.

那么我们不允许这样做的原因是什么?

c# scope language-design

47
推荐指数
4
解决办法
3万
查看次数

Tensorflow:如何通过名称获得张量?

我无法通过名字恢复张量,我甚至不知道它是否可能.

我有一个创建我的图形的函数:

def create_structure(tf, x, input_size,dropout):    
 with tf.variable_scope("scale_1") as scope:
  W_S1_conv1 = deep_dive.weight_variable_scaling([7,7,3,64], name='W_S1_conv1')
  b_S1_conv1 = deep_dive.bias_variable([64])
  S1_conv1 = tf.nn.relu(deep_dive.conv2d(x_image, W_S1_conv1,strides=[1, 2, 2, 1], padding='SAME') + b_S1_conv1, name="Scale1_first_relu")
.
.
.
return S3_conv1,regularizer
Run Code Online (Sandbox Code Playgroud)

我想访问此函数外的变量S1_conv1.我试过了:

with tf.variable_scope('scale_1') as scope_conv: 
 tf.get_variable_scope().reuse_variables()
 ft=tf.get_variable('Scale1_first_relu')
Run Code Online (Sandbox Code Playgroud)

但这给了我一个错误:

ValueError:欠共享:变量scale_1/Scale1_first_relu不存在,不允许.你是不是要在VarScope中设置reuse = None?

但这有效:

with tf.variable_scope('scale_1') as scope_conv: 
 tf.get_variable_scope().reuse_variables()
 ft=tf.get_variable('W_S1_conv1')
Run Code Online (Sandbox Code Playgroud)

我可以解决这个问题

return S3_conv1,regularizer, S1_conv1
Run Code Online (Sandbox Code Playgroud)

但我不想那样做.

我认为我的问题是S1_conv1实际上不是一个变量,它只是一个张量.有办法做我想要的吗?

python scope tensorflow

47
推荐指数
2
解决办法
6万
查看次数

python中奇怪的作用域行为

考虑以下python代码片段:

x = 1
class Foo:
    x = 2
    def foo():
        x = 3
        class Foo:
            print(x) # prints 3
Foo.foo()
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,这会打印 3。但是,如果我们在上面的代码片段中添加一行,行为就会改变:

x = 1
class Foo:
    x = 2
    def foo():
        x = 3
        class Foo:
            x += 10
            print(x) # prints 11
Foo.foo()
Run Code Online (Sandbox Code Playgroud)

而且,如果我们在上面的例子中切换两行的顺序,结果又会发生变化:

x = 1
class Foo:
    x = 2
    def foo():
        x = 3
        class Foo:
            print(x) # prints 1
            x += 10
Foo.foo()
Run Code Online (Sandbox Code Playgroud)

我想了解为什么会发生这种情况,更一般地说,了解导致这种行为的范围规则。根据 LEGB 范围规则,我希望两个片段都打印 3、13 和 3,因为x在封闭函数中定义了一个foo()

python scope python-3.x

47
推荐指数
1
解决办法
2474
查看次数

函数和局部变量可以同名吗?

这是我的意思的一个例子:

def foo():
    foo = 5
    print(foo + 5)

foo()
# => 10
Run Code Online (Sandbox Code Playgroud)

该代码不会产生任何错误并且运行完美。这与变量和函数不应具有相同名称(除非覆盖它们)的想法相矛盾。为什么它有效?当应用于实际代码时,我应该使用不同的函数/局部变量名称,还是这完全没问题?

python scope

47
推荐指数
3
解决办法
6611
查看次数

AngularJS:如何将参数/函数传递给指令?

看看这个小提琴,我需要改变什么,模板中的表达式使用我在HTML中定义的参数进行评估?SAVE按钮应该调用blabla()控制器的-function,因为我通过它?

var myApp = angular.module('MyApp',[])
myApp.directive('editkeyvalue', function() {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            accept: "expression"
        },
        template : '<div><label class="control-label">{{key}}</label>' +
        '<label class="control-label">{{key}}</label>' +
          '<input type="text" ng-model="value" />'+
        '<button type="button" x-ng-click="cancel()">CANCEL</button>' +
        '<button type="submit" x-ng-click="save()">SAVE</button></div>',

      controller: function($scope, $element, $attrs, $location) {
        $scope.save= function() {
          $scope.accept();
        };
      }
    }
});
Run Code Online (Sandbox Code Playgroud)

我真的没有看透.感谢帮助!

javascript html5 scope controller angularjs

46
推荐指数
2
解决办法
10万
查看次数

scala类构造函数参数

有什么区别:

class Person(name: String, age: Int) {
  def say = "My name is " + name + ", age " + age
}
Run Code Online (Sandbox Code Playgroud)

class Person(val name: String, val age: Int) { 
  def say = "My name is " + name + ", age " + age
}
Run Code Online (Sandbox Code Playgroud)

我可以将参数声明为vars,并在以后更改它们的值吗?例如,

class Person(var name: String, var age: Int) {

  age = happyBirthday(5)

  def happyBirthday(n: Int) {
    println("happy " + n + " birthday")
    n
  }
}
Run Code Online (Sandbox Code Playgroud)

constructor scope scala immutability

46
推荐指数
3
解决办法
3万
查看次数

为什么使用自动生成文件句柄的三参数打开调用是Perl的最佳实践?

我有两个关于Perl open函数的问题:

1)我似乎记得Perl最佳实践中的3参数版本open比两个参数版本更好,例如

open(OUT, '>>', $file);
Run Code Online (Sandbox Code Playgroud)

open(OUT, ">>$file");
Run Code Online (Sandbox Code Playgroud)

这是为什么?我试图告诉别人前几天使用3参数版本,但似乎无法用任何东西支持它.

2)我似乎还记得autovivified文件句柄比bareword文件句柄更受青睐(他们称之为不同的东西)?而且也记不住为什么,例如

open(my $out, '>>', $file);
Run Code Online (Sandbox Code Playgroud)

open(OUT, '>>', $file);
Run Code Online (Sandbox Code Playgroud)

这是strict件事吗?我似乎记得能够使用OUT,strict但我不记得了.

perl file-io scope

45
推荐指数
3
解决办法
6055
查看次数

Angular JS可调整大小的div指令

我的网站将有多个部分,每个部分我打算可以调整大小.为了实现这一点,我制定了一个"可调整大小"的指令,例如:

<div class="workspace" resize="full" ng-style="resizeStyle()">
<div class="leftcol" resize="left" ng-style="resizeStyle()">
Run Code Online (Sandbox Code Playgroud)

使用类似于以下内容的指令:

lwpApp.directive('resize', function ($window) {
    return {
        scope: {},

        link: function (scope, element, attrs) {
            scope.getWinDim = function () {
                return {
                    'height': window.height(),
                    'width': window.width()
                };
            };

            // Get window dimensions when they change and return new element dimensions
            // based on attribute
            scope.$watch(scope.getWinDim, function (newValue, oldValue) {
                scope.resizeStyle = function () {
                    switch (attrs.resize) {
                    case 'full':
                        return {
                            'height': newValue.height,
                            'width': (newValue.width - dashboardwidth)
                        };

                    case 'left':
                        return …
Run Code Online (Sandbox Code Playgroud)

scope resize controller directive angularjs

45
推荐指数
2
解决办法
7万
查看次数