built-in-types

Inheriting behaviours for set and frozenset seem to differ

牧云@^-^@ 提交于 2019-12-07 00:08:59
问题 Can someone explain the following behaviour: class derivedset1(frozenset): def __new__(cls,*args): return frozenset.__new__(cls,args) class derivedset2(set): def __new__(cls,*args): return set.__new__(cls,args) a=derivedset1('item1','item2') # WORKS b=derivedset2('item1','item2') # DOESN'T WORK Traceback (most recent call last): File "inheriting-behaviours.py", line 12, in <module> b=derivedset2('item1','item2') # DOESN'T WORK TypeError: derivedset2 expected at most 1 arguments, got 2 This is

Do primitive types have also constructors in C++?

六眼飞鱼酱① 提交于 2019-12-06 02:16:34
问题 I have read in Dr. Bjarne Stroustrup Book "The C++ Programming Language" 3rd edition that built in types have also constructors in C++ in section 10.4.2. But then the following link says that POD types can't have constructors: http://www.parashift.com/c++-faq-lite/pod-types.html Which is true? Do primitive types have also constructors in C++? 回答1: What Bjarne means is that you can write int(56) or even int() to construct an integer. What the links means is that a struct/class is only a POD if

Custom comparison functions for built-in types in Python

拜拜、爱过 提交于 2019-12-05 10:17:15
I am using Python's built-in sets to hold objects of a class I have defined. For this class, I defined __eq__ , __ne__ , and __hash__ so that I can compare objects by my custom comparison functions. That works just fine, until I find out that I actually need two sets of comparison functions, which will be used in different ways at different times in my code. I can't define two sets of __eq__ , etc. methods in my class, and Python's built-in set type does not accept a comparator argument. I suppose I could go write a wrapper class around set, but that seems like a lot more work than necessary.

How do I value-initialize a Type* pointer using Type()-like syntax?

假装没事ソ 提交于 2019-12-04 23:38:17
Variables of built-in types can be value-initialized like this : int var = int(); this way I get the default value of int without hardcoding the zero in my code. However if I try to do similar stuff for a pointer: int* ptr = int*(); the compiler (Visual C++ 10) refuses to compile that (says type int unexpected ). How do I value-initialize a pointer in similar manner? How do I value-initialize a Type* pointer using Type()-like syntax? You cannot. The syntax T() is defined in 5.2.3/1,2 (C++03, slightly different wording in C++11 FDIS). In particular the second paragraph states: The expression T(

Redefining Pythons builtin datatypes

ε祈祈猫儿з 提交于 2019-12-04 11:40:38
Is it possible to redefine which object the brackets [] use? I can subclass the list object, but how to I make the interpreter use my subclass in place of the buildin list object? Is it possible? (I'm pretty sure I'm using the wrong terms for the question- feel free to edit) >>> class mlist(list): ... def __init__(self): ... list.__init__(self) ... def __getitem__(self, item): ... return list.__getitem__(self, item) * 2 ... >>> testlist = mlist() >>> testlist.append(21) >>> testlist[0] 42 >>> list = mlist() # maybe setting the 'list' type will do it? >>> testlist = [] >>> testlist.append(21) >

Do primitive types have also constructors in C++?

为君一笑 提交于 2019-12-04 05:48:24
I have read in Dr. Bjarne Stroustrup Book "The C++ Programming Language" 3rd edition that built in types have also constructors in C++ in section 10.4.2. But then the following link says that POD types can't have constructors: http://www.parashift.com/c++-faq-lite/pod-types.html Which is true? Do primitive types have also constructors in C++? What Bjarne means is that you can write int(56) or even int() to construct an integer. What the links means is that a struct/class is only a POD if it does not have a constructor declared. So Bjarne talks about primitive non-struct types and the link

int a=int(); what happens in C++98?

孤街浪徒 提交于 2019-12-04 03:16:10
Please read the question entirely before you think to mark it as duplicate. The statement like int i=int(); most programmers will say that there is value initialization here & i will be value initialized. (0 as output). But it also prints 0 as output on C++98 compiler. Following program that I tested on C++98 implementation and gives me 0 as output. #include <iostream> int main() { int i=int(); std::cout<<i; } Don't say that i is value initialized in above C++98 program ,because value initialization introduced in C++03. So How i is initialized here? Is it really constructor call? int() looks

How is “int* ptr = int()” value initialization not illegal?

荒凉一梦 提交于 2019-12-02 16:02:57
The following code (taken from here ): int* ptr = int(); compiles in Visual C++ and value-initializes the pointer. How is that possible? I mean int() yields an object of type int and I can't assign an int to a pointer. How is the code above not illegal? int() is a constant expression with a value of 0, so it's a valid way of producing a null pointer constant. Ultimately, it's just a slightly different way of saying int *ptr = NULL; Because int() yields 0 , which is interchangeable with NULL . NULL itself is defined as 0 , unlike C's NULL which is (void *) 0 . Note that this would be an error:

problem subclassing builtin type

风流意气都作罢 提交于 2019-12-01 17:59:19
# Python 3 class Point(tuple): def __init__(self, x, y): super().__init__((x, y)) Point(2, 3) would result in TypeError: tuple() takes at most 1 argument (2 given) Why? What should I do instead? tuple is an immutable type. It's already created and immutable before __init__ is even called. That is why this doesn't work. If you really want to subclass a tuple, use __new__ . >>> class MyTuple(tuple): ... def __new__(typ, itr): ... seq = [int(x) for x in itr] ... return tuple.__new__(typ, seq) ... >>> t = MyTuple((1, 2, 3)) >>> t (1, 2, 3) 来源: https://stackoverflow.com/questions/4827303/problem

problem subclassing builtin type

柔情痞子 提交于 2019-12-01 17:08:46
问题 # Python 3 class Point(tuple): def __init__(self, x, y): super().__init__((x, y)) Point(2, 3) would result in TypeError: tuple() takes at most 1 argument (2 given) Why? What should I do instead? 回答1: tuple is an immutable type. It's already created and immutable before __init__ is even called. That is why this doesn't work. If you really want to subclass a tuple, use __new__. >>> class MyTuple(tuple): ... def __new__(typ, itr): ... seq = [int(x) for x in itr] ... return tuple.__new__(typ, seq