标签: mutable

Python对象初始化错误.或者我误解了对象是如何工作的?

  1 import sys
  2 
  3 class dummy(object):
  4     def __init__(self, val):
  5         self.val = val
  6 
  7 class myobj(object):
  8     def __init__(self, resources):
  9         self._resources = resources
 10 
 11 class ext(myobj):
 12     def __init__(self, resources=[]):
 13         #myobj.__init__(self, resources)
 14         self._resources = resources
 15 
 16 one = ext()
 17 one._resources.append(1)
 18 two = ext()
 19 
 20 print one._resources
 21 print two._resources
 22 
 23 sys.exit(0)
Run Code Online (Sandbox Code Playgroud)

这将打印的参考对象分配one._resources两个onetwo对象.我认为这two将是一个空数组,因为如果在创建对象时没有定义它,它就会明确地设置它.取消注释也会myobj.__init__(self, resources)做同样的事情.使用super(ext, self).__init__(resources)也做同样的事情.

我可以使用它的唯一方法是使用以下方法:

two …
Run Code Online (Sandbox Code Playgroud)

python arguments mutable

1
推荐指数
2
解决办法
242
查看次数

在java中获取一个字符串作为引用

所以我希望能够在Java中拥有一组可变的字符串.

我有这个测试类来查看不可变字符串的功能:

public class GetStringTest
{
    private Vector<String> m_stringList;

    public GetStringTest()
    {
        m_stringList = new Vector<String>();

        m_stringList.add("zero");
        m_stringList.add("one");
        m_stringList.add("two");
        m_stringList.add("three");
        m_stringList.add("four");
        m_stringList.add("five");
        m_stringList.add("six");
    }

    public String getString(int index)
    {
        return m_stringList.get(index);
    }

    public String toString()
    {
        String str = "";

        for (String item : m_stringList)
        {
            str += item + "\n";
        }

        return str;
    }

    public static void main(String[] args)
    {
        GetStringTest gst = new GetStringTest();

        System.out.println("=== original content ===");
        System.out.println(gst);

        String strToChange = gst.getString(2); // "two"
        strToChange = "eleventy-one"; …
Run Code Online (Sandbox Code Playgroud)

java string mutable immutability

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

Python,可变对象作为默认参数,有什么方法可以解决?

class A:
    def __init__(self, n=[0]):
        self.data = n

a = A()
print a.data[0] #print 0
a.data[0] +=1

b = A()
print a.data[0] #print 1, desired output is 0
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,有没有办法在__init __()类A中提供带有可变对象(如列表或类)的默认参数,但是b不受操作a的影响?

python class object mutable

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

ocaml - 具有可变字段的记录数组

我是OCaml的新手,我正在努力理解它的概念mutable record field.

我想创建一个记录数组,该记录包含一个布尔可变字段.所以我做了类似的事情:

type t = {i: int; mutable b: bool};;
Run Code Online (Sandbox Code Playgroud)

我希望能够更改记录的'b'字段的值,所以我把它 mutable

let m = Array.make 10 ({i = 5; b = false});;
Run Code Online (Sandbox Code Playgroud)

在这里,我尝试设置位于我的数组的第一个索引处的记录的b字段:

(Array.get m 0).b <- true;;
Run Code Online (Sandbox Code Playgroud)

问题是我想要它,它将设置数组的所有记录的'b'字段,这不是我想要的.

同一记录的可变字段是否共享相同的内存位置?如何更改特定记录的"b"字段的值?

ocaml records mutable

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

对可变数据的可变引用

我经常听到"对不可变数据的可变引用"这个术语.就我而言,这是针对Scala的.如果你有一个可变引用,这不意味着不可变数据是可变的吗?我很难理解它的理论和实践方面.例子很棒.

concurrency functional-programming scala mutable immutability

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

如何更改Python切片时,如何使Python列表可变

Python的切片操作会创建列表的指定部分的副本.如何传递父列表的切片,以便在此切片更改时,父列表的相应部分随之更改?

def modify(input):
    input[0] = 4
    input[1] = 5
    input[2] = 6


list = [1,2,3,1,2,3]
modify(list[3:6])
print("Woud like to have: [1,2,3,4,5,6]")
print("But I got: "  + str(list))
Run Code Online (Sandbox Code Playgroud)

输出:

想拥有:[1,2,3,4,5,6]
但我得到了:[1,2,3,1,2,3]

python list mutable

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

如何对不可变值进行可变引用

我刚刚开始学习一点Rust,并且对变量的可变性概念非常感兴趣.

我正在尝试编写与此C++程序非常相似的内容.

#include <cstdio>

void do_something(int &var) {
   var++;
}

int main() {

    int a = 3;
    int b = a;
    printf("a is %d and b is %d\n", a, b);
    do_something(b);
    printf("a is %d and b is %d\n", a, b);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望看到:

a是3,b是3

a是3,b是4

这个想法是传递引用呈现b可变,但a不可变.

以下是我假设在Rust中编写此程序的方法:

fn main() {
    let a: i32 = 3;
    let b: &mut i32 = &a;

    println!("a is {} and b is {}", a, b);
    do_something(b);
    println!("a is {} and …
Run Code Online (Sandbox Code Playgroud)

mutable pass-by-reference rust

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

是否有语法来防止类的实例是const?

假设我创建了一个类,其中主要用例将使用户始终调用修改其成员的方法.或者,以另一种方式查看它,创建一个类,其中每个方法都将修改类成员.

例如,让我们使用这个虚拟类:

class Foo
{
public:
    void setM_1(int);
    void setM_2(char);
    void setM_3(float);

private:
    int     m_1;
    char    m_2;
    float   m_3;
};
Run Code Online (Sandbox Code Playgroud)

对于这个Foo类,创建const它的实例没有意义,因为每个方法都保证修改成员.

我的目标是:以这样的方式定义这个类 - const实例化这个类将没有任何效果.也就是说,const Foo实例可以调用Foo实例可以的每个方法.

通过标记每个方法const,声明所有非const成员mutable,并提供初始化类的所有成员的ctor,我能够实现此行为.

所以const-ignorant版本Foo看起来像:

class Foo
{
public:
    Foo()
    {
        m_1 = 0;
        m_2 = '\0';
        m_3 = 0.0f;
    }

    void setM_1(int)   const;
    void setM_2(char)  const;
    void setM_3(float) const;

private:
    mutable int     m_1;
    mutable char    m_2;
    mutable float …
Run Code Online (Sandbox Code Playgroud)

c++ const class mutable c++11

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

如何修改或读取在函数中作为参数传递的可变向量?

  test :: VM.MVector s Int -> Int
  test x = runST $ do
    a <- return x
    VM.read a 0 -- Type error
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚如何不将ST monad中的所有内容都放入单个函数中.如果我试图修改x或返回一个值,编译器会抱怨可变向量的状态部分不匹配.

是否可以在Haskell中对传递的可变向量进行操作,或者在对它们执行任何操作之前是否必须将它们冻结为不可变对应的?

编辑:

这是实际的错误.

Couldn't match type `s1' with `s'
  `s1' is a rigid type variable bound by
       a type expected by the context: ST s1 Int at rjb.hs:17:12
  `s' is a rigid type variable bound by
      the type signature for test :: VM.MVector s Int -> Int
      at rjb.hs:16:11
Expected type: VM.MVector
                 (Control.Monad.Primitive.PrimState …
Run Code Online (Sandbox Code Playgroud)

monads haskell vector mutable

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

我何时可以并行使用可变变量来调用函数?

看完一个有趣的讲座后 Phil Trelford

https://www.youtube.com/watch?v=hx2vOwbB-X0

我对通过用数组替换列表以及更一般地使用可变变量来加速我的代码的可能性很感兴趣.所以我做了一个简单的测试:

let xs = [0.0..1000000.00]
let axs = List.toArray xs

let f x = sin (x * x)

List.map f xs // Real: 00:00:00.170, CPU: 00:00:00.187, GC gen0: 5, gen1: 3, gen2: 1

Array.map f axs // Real: 00:00:00.046, CPU: 00:00:00.046, GC gen0: 0, gen1: 0, gen2: 0
Run Code Online (Sandbox Code Playgroud)

通过数组映射比通过列表映射快三倍以上.此时,当调用的函数计算量更大时,我还没有测试速度差异.差异可能仅仅是因为更快地遍历数组中的项目,并且当每次迭代都是计算密集时可能变得无关紧要.

但是,必须存在使用数组或更普遍的可变变量才能产生显着差异的情况.

在更改我的代码以使用数组而不是列表之前,我想更清楚地了解代码并行化时的后果.

一般来说,什么时候可以使用可变变量而不会冒并行代码的问题?是否有一个简单的测试可以让我在并行调用时确定函数的稳健性?

parallel-processing f# mutable

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