小编Sri*_*ini的帖子

主函数是否可以成为类的朋友?

我知道这对数据隐藏是有害的,但理论上这是允许的吗?

c++

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

初始化Moose类的CodeRef字段

我有一个穆斯班 Person

 package Person;

  use Moose;

  has 'first_name' => (
      is  => 'rw',
      isa => 'Str',
  );

  has 'last_name' => (
      is  => 'rw', 
      isa => 'Str',
  );

  has 'check' => (
      is => 'rw',
      isa => 'CodeRef',
  );

  no Moose;
  __PACKAGE__->meta->make_immutable;
Run Code Online (Sandbox Code Playgroud)

我正在初始化Person另一个文件中的新对象

use Person;

my $user = Person->new(
    first_name => 'Example',
    last_name  => 'User',
    check => sub {
        print "yo yo\n";
    },
  ); 

print "here\n";
$user->check();
print "here\n";
Run Code Online (Sandbox Code Playgroud)

两个here调试语句正在打印,但子例程中的调试消息不是.

我想知道将函数传递给构造函数的正确方法,以便我可以将匿名子例程传递给对象.

perl moose

5
推荐指数
2
解决办法
243
查看次数

How to copy some elements from a list-of-list so that is doesn't affect the value of elements in the copied list

What I want to do is to copy some elements of one list-of-list to other based on certain conditions and then change the original list of lists

arr = [[1,0,4],[1,2,65],[2,3,56],[11,14,34]]
brr = []

for x in range(0,len(arr)):
    if arr[x][1] < 10:
        brr.append(arr[x])
        arr[x][1] = 1000
print(brr)
Run Code Online (Sandbox Code Playgroud)

O/P:

[[1, 1000, 4], [1, 1000, 65], [2, 1000, 56]]

in the above example, I wanted to copy all the list with the middle element <10 to another list-of-list brr and then change the …

python

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

Python、adb 和 shell 执行查询

我对 Python 不太熟悉,但我对 Ruby 很熟悉。所以我将为我想要实现的目标提供类似物

在 Ruby 中,我会

val = `adb devices`
Run Code Online (Sandbox Code Playgroud)

获取存储在 val 中的 adb 设备的“原始输出”,以及

val=system("adb devices")
Run Code Online (Sandbox Code Playgroud)

获取状态码

我想在 Python 中执行相同的任务。我在看

from subprocess import call
call(["adb devices"])
Run Code Online (Sandbox Code Playgroud)

但这失败了,我不想使用 os.system 因为我想获得一些正确的错误处理。我如何使调用工作以及如何从 Python 中的反引号获取原始输出

python adb

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

尾递归解决方案的相反是什么?

我正在解决Ocaml 99 题中的第 4 题中的问题 4 ,并且我仍在学习 OCaml

问题 4 读为

OCaml 标准库有 List.length 但我们要求您重新实现它。尾递归解决方案的奖励。

因此,根据我的理解,递归解决方案值得加分,因为它比可能更简单、更冗长的解决方案更有效

我想出了这个尾递归解决方案

let length in_list =
  let rec find_len cur_length = function
  | [] -> cur_length
  | hd::tl -> find_len (cur_length + 1) tl
  in find_len 0 in_list
;;
Run Code Online (Sandbox Code Playgroud)

根据我对尾递归的理解,这是有效的,因为它在尾部递归操作

我的问题是这的反面是什么?什么是有效的非尾递归解决方案

我想这将是在我想出的列表的头部递归运行的东西

let hd_rec_length in_list =
  let rec pop_last saved_list= function
  | [] -> saved_list
  | [last] -> saved_list
  | hd::tl -> pop_last (saved_list@[hd]) tl
  in 
    let rec hd_rec_find_len cur_length in_list =
    match …
Run Code Online (Sandbox Code Playgroud)

ocaml functional-programming

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

Bash退格字符正确使用

我做了一件非常简单的事情.

echo -e "123\b4" >> simple
cat simple
124
Run Code Online (Sandbox Code Playgroud)

但是当我在Sublime文本中打开这个文件时,我会得到一些名为"BS"的字符,它可能代表退格,当我用gedit打开它时,我会得到一个空格而且空格后有4个空格.这是文件的正确版本?

当我用vim在命令行上打开文件时,我得到了一个

^H
Run Code Online (Sandbox Code Playgroud)

而不是字符.

有人可以提供指导吗?

linux bash command-line

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

酸洗物体的错误

我们正在用泡菜做练习,这段代码不能正常运行,请帮助我,这是代码(有些单词是西班牙语,因为我来自美国):

    import pickle

class persona:

    def __init__(self, nombre, genero, edad):
        self.nombre = nombre
        self.genero = genero
        self.edad = edad
        print("se ha creado una persona nueva con el nombre de: ", self.nombre)

    def __str__(self):
        return "{} {} {}".format(self.nombre, self.genero, self.edad)

class listaPersonas:

    personas = []

    def __init__(self):
        listaDePersonas = open("ficheroExterno", "ab+")
        listaDePersonas.seek(0)

        try:
            self.personas = pickle.load(listaDePersonas)
            print("Se cargaron {} personas del fichero externo".format(len(self.personas)))
        except:
            print("El fichero está vacío")

        finally:
            listaDePersonas.close()
            del(listaDePersonas)

    def agregarPersonas(self, p):
        self.personas.append(p)
        self.guardarPersonasEnFicheroExterno()

    def mostrarPersonas(self):
        for p in …
Run Code Online (Sandbox Code Playgroud)

python pickle

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

标签 统计

python ×3

adb ×1

bash ×1

c++ ×1

command-line ×1

functional-programming ×1

linux ×1

moose ×1

ocaml ×1

perl ×1

pickle ×1