小编Ant*_*osa的帖子

__new__、__init__ 和元类(和超类)

我花了一天的时间试图理解 python 类模型的复杂性,弄乱了装饰器、元类和超类。

目前,我正在尝试弄清楚某些令牌函数的作用,即新的(这里的背景故事元类以及何时/如何调用函数

我制作了一个新的模型模块来运行测试,在这里:

#! /usr/bin/env python3

import sys as system
import os  as operating_system

from functools import partial
from time      import perf_counter as counter

class Meta(type):

    @classmethod
    def __prepare__(instance, name, supers, *list, **map):
        print('{} in meta prepare'.format(name))
        return {}

    def __new__(instance, name, supers, attributes, *list, **map):
        print('{} in meta new'.format(name))
        return instance

    def __init__(self, name, supers, attributes, *list, **map):
            print('{} in meta init'.format(self))

    def __call__(self, *list, **map):
        print('{} in meta call'.format(self))
        return type.__call__(self)
        print('after …
Run Code Online (Sandbox Code Playgroud)

inheritance metaprogramming metaclass class python-3.x

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

打字稿通用专业化

我正在寻找类似于 Typescript 泛型专业化的东西,其中实现可以根据类型标准脱节。

一个最小的例子:

const someFunction = <A>() => { return 0; }

// something like this
<A extends String>someFunction = (a: A) => { return 1; }
<A extends Number>someFunction = (a: A) => { return 2; }
.
.
.

console.log(someFunction(false)); // prints 0
console.log(someFunction('string')); // prints 1
console.log(someFunction(42)); // prints 2
Run Code Online (Sandbox Code Playgroud)

这是我想要的“jist”。这在打字稿中可能吗?

generics typescript

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

TS Concepts typename约束

我试图用一个概念作为子类的约束(由gcc与gnu2a和fconcepts编译)来创建一个模板化继承的简单例子.我期望下面的例子编译得很好,但我无法让它工作:

template<class structure>
concept bool Has_Type() {return requires{
    typename structure::type;
};}

template<class sub> requires Has_Type<sub>()
struct structure {
     //using type = typename sub::type;
};

struct child: structure<child> {
    using type = child;
};
Run Code Online (Sandbox Code Playgroud)

这个概念引发了一个错误typename structure::type would be ill-formed.我不明白为什么,因为孩子的类型可由::操作员访问.我试过这个例子来看看这个想法本身是否有效,并且编译并运行良好:

struct child {
    using type = child;
};

template<class it>
auto func() {
    typename it::type f = child();
    return 0;
}

// in a test.cpp file

auto g = func<child>();
Run Code Online (Sandbox Code Playgroud)

这让我觉得这个想法得到了支持,所以我不确定为什么这个概念失败了.谁知道为什么?

c++ inheritance templates typename c++-concepts

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

在脚本中定义新的fish命令并解析参数

快问。是否可以通过在 Fish 中编写脚本来定义新命令?我想按照以下方式编写一个新命令:

newFunction --flag1 flag1Input --flag2 flag2Input then space delimited arguments
Run Code Online (Sandbox Code Playgroud)

这个新函数只能以函数的方式使用已经定义的命令,我只是希望能够将标志传递给这个新函数。这可能吗,还是我必须使用另一种语言(例如 Python)来为我的假设命令创建可执行文件?

fish

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

返回本地对象的元组

如何利用结构化绑定和元组来返回函数本地对象?

在函数中,我正在创建相互引用的本地对象,并且我希望在元组中返回这些对象,并在调用函数时使用结构化绑定来识别它们。我目前有这个:

std::tuple<Owner&&, State<Controller>&&, State<Ancillary>&&, State<Compressor>&&>
inline makeOwner() {
    State<Controller>&& controller = State<Controller>();
    State<Ancillary>&&  ancillary  = State<Ancillary>();
    State<Compressor>&& compressor = State<Compressor>();

    Owner&& owner = Owner(controller, ancillary, compressor);

    return {owner, controller, ancillary, compressor};
}

// using the function later

const &&[owner, controller, ancillary, compressor] = makeOwner();
Run Code Online (Sandbox Code Playgroud)

这不起作用,并且我收到一条错误消息,指出返回值无法转换为上述返回类型的元组。我不确定为什么会出现这种情况,因为类型与声明相匹配。

最终,我试图创建一个方便的函数,这样我就不必每次想要创建新的所有者时都在函数中键入四行。这是我尝试使用结构化绑定来使这变得更容易。

编辑:我应该注意,我希望最后一行中的绑定引用所有者内部的对象。所以,副本是不够的。

c++ templates move-semantics c++17 structured-bindings

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

元类以及何时/如何调用函数

我正在尝试学习元类如何在python 3中工作.我想知道的是:调用哪些函数,按什么顺序,以及它们的签名和返回.

作为一个例子,我知道__prepare__当带有元类的类用参数实例化metaclass, name_of_subclass, bases并返回表示实例化对象的未来命名空间的字典时,会调用它.

我觉得我理解__prepare__这个过程中的一步.我不这样做,虽然,是__init__,__new____call__.他们的论点是什么?他们回报了什么?他们如何互相称呼,或者一般如何进行?目前,我一直在理解何时__init__被召唤.

这是我一直在讨论的一些代码来回答我的问题:

#!/usr/bin/env python3

class Logged(type):

    @classmethod
    def __prepare__(cls, name, bases):
        print('In meta __prepare__')
        return {}

    def __call__(subclass):
        print('In meta __call__')
        print('Creating {}.'.format(subclass))
        return subclass.__new__(subclass)

    def __new__(subclass, name, superclasses, attributes, **keyword_arguments):
        print('In meta __new__')
        return type.__new__(subclass, name, superclasses, attributes)

    def __init__(subclass, name, superclasses, attributes, **keyword_arguments):
        print('In meta __init__')

class Thing(metaclass = Logged):

    def __new__(this, *arguments, **keyword_arguments):
        print('In sub __new__')
        return super(Thing, …
Run Code Online (Sandbox Code Playgroud)

python metaprogramming metaclass python-3.x

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

C++ 构造函数内部会发生什么?

我想知道在 C++ 中调用类的构造函数时会发生什么。更具体地说,调用了哪些其他函数以及如何调用。是调用某个内置函数在栈上构造对象,还是调用malloc在堆上构造对象new?是否有使用的默认分配器?

一般来说,是否可以在不调用构造函数的情况下使用类和对象?

编辑:一个可能有助于我试图解决的好问题是:为什么构造函数不返回任何东西?最初,我认为新手程序员会期望构造函数返回对象,但事实并非如此。会发生什么?

c++ constructor allocation

0
推荐指数
2
解决办法
398
查看次数