overloading

Can bin() be overloaded like oct() and hex() in Python 2.6?

只愿长相守 提交于 2019-12-03 02:05:52
In Python 2.6 (and earlier) the hex() and oct() built-in functions can be overloaded in a class by defining __hex__ and __oct__ special functions. However there is not a __bin__ special function for overloading the behaviour of Python 2.6's new bin() built-in function. I want to know if there is any way of flexibly overloading bin() , and if not I was wondering why the inconsistent interface? I do know that the __index__ special function can be used, but this isn't flexible as it can only return an integer. My particular use case is from the bitstring module, where leading zero bits are

Methods with the same name in one class in python?

白昼怎懂夜的黑 提交于 2019-12-03 01:28:48
问题 How to declare few methods with the same name ,but with different numbers of parameters or different types in one class? What I must to change in this class: class MyClass: """""" #---------------------------------------------------------------------- def __init__(self): """Constructor""" def my_method(self,parameter_A_that_Must_Be_String): print parameter_A_that_Must_Be_String def my_method(self,parameter_A_that_Must_Be_String,parameter_B_that_Must_Be_String): print parameter_A_that_Must_Be

Overloading multiple function objects by reference

非 Y 不嫁゛ 提交于 2019-12-03 01:02:44
问题 In C++17, it is trivial to implement an overload(fs...) function that, given any number of arguments fs... satisfying FunctionObject, returns a new function object that behaves like an overload of fs... . Example: template <typename... Ts> struct overloader : Ts... { template <typename... TArgs> overloader(TArgs&&... xs) : Ts{forward<TArgs>(xs)}... { } using Ts::operator()...; }; template <typename... Ts> auto overload(Ts&&... xs) { return overloader<decay_t<Ts>...>{forward<Ts>(xs)...}; } int

Overloading operator-() in C++

≯℡__Kan透↙ 提交于 2019-12-03 00:40:34
问题 I am trying to convert the following function to include - sign instead of a subtract function. But it says function not viable and expects an lvalue for the 1st argument. class fraction { long num; long den; } I have excluded the constructor and everything else. inline fraction fraction::operator+(fraction &f) { fraction t; t.num = num - f.num; t.den = den - f.den; return t; } inline const fraction sub(const fraction& f,const fraction& s) { return fraction (getNum(f)*getDen(s)-getDen(f)

Java Constructor Overloading

大城市里の小女人 提交于 2019-12-02 23:53:07
问题 I'm new with Java and I'm having trouble understanding the constructor issue, I have looked at many tutorials and still I'm having difficult to understand why we use constructors, anyway, my specific question is : Correct me if I'm wrong, if i want to add in my class more than one constructor, I'll write the first one and the second will be int type (inside the brackets). is it because the constructors have to be with the same name as the class and we need to distinguish between them ? what

Overloaded constructor calling other constructor, but not as first statement

跟風遠走 提交于 2019-12-02 22:51:41
I'm having some trouble using multiple constructors in java. what I want to do is something like this: public class MyClass { // first constructor public MyClass(arg1, arg2, arg3) { // do some construction } // second constructor public MyClass(arg1) { // do some stuff to calculate arg2 and arg3 this(arg1, arg2, arg3); } } but I can't, since the second constructor cannot call another constructor, unless it is the first line. What is the common solution for such situation? I can't calculate arg2 and arg3 "in line". I thought maybe creating a construction helper method, that will do the actual

Why was function overloading added to C++?

拥有回忆 提交于 2019-12-02 21:57:51
I have a C background. I was just wondering why was function overloading added to C++? C doesn't have function overloading but C++ does, what was the need for it? What went across the mind of the language designer at that time? It increases maintainability. If you have a type T and you call a function with it, then you need to change T, if the function has been overloaded for the new T then you can recompile instantly. In C you would have to go back and dig through all the call sites and change the function called. Take sqrt(). If you want to sqrt() a float, then you have to change to sqrtf().

Java static imports

不打扰是莪最后的温柔 提交于 2019-12-02 20:15:45
Just by experiment I discovered that Java non static methods overrides all same named methods in scope even at static context. Even without allowing parameter overloading. Like import java.util.Arrays; import static java.util.Arrays.toString; public class A { public static void bar(Object... args) { Arrays.toString(args); toString(args); //toString() in java.lang.Object cannot be applied to (java.lang.Object[]) } } I can't find anything about this in spec. Is this a bug? If it isn't, are there any reasons to implement language like that? UPD: Java 6 do not compile this example. The question is

Why do primitive and user-defined types act differently when returned as 'const' from a function?

巧了我就是萌 提交于 2019-12-02 20:03:54
#include <iostream> using namespace std; template<typename T> void f(T&&) { cout << "f(T&&)" << endl; } template<typename T> void f(const T&&) { cout << "f(const T&&)" << endl; } struct A {}; const A g1() { return {}; } const int g2() { return {}; } int main() { f(g1()); // outputs "f(const T&&)" as expected. f(g2()); // outputs "f(T&&)" not as expected. } The issue description is embedded in the code. My compiler is clang 5.0 . I just wonder: Why does C++ treat built-in types and custom types differently in such a case? I don't have a quote from the standard, but cppreference confirms my

How to overload Python's __bool__ method? [duplicate]

我是研究僧i 提交于 2019-12-02 20:00:06
Possible Duplicate: defining “boolness” of a class in python I thought this should print "False", why is it printing "True"? >>> class Foo(object): ... def __bool__(self): ... return False ... >>> f = Foo() >>> if f: ... print "True" ... else: ... print "False" ... True >>> You should define __nonzero__() in Python 2.x. It was only renamed to __bool__() in Python 3.x. (The name __nonzero__() actually predates the introduction of the bool type by many years.) 来源: https://stackoverflow.com/questions/8909932/how-to-overload-pythons-bool-method