wrapper

Assigning (instead of defining) a __getitem__ magic method breaks indexing [duplicate]

送分小仙女□ 提交于 2019-11-26 10:03:15
问题 This question already has answers here : Why won't dynamically adding a `__call__` method to an instance work? (2 answers) Closed 11 months ago . I have a wrapper class similar to this (strongly simplified) example: class wrap(object): def __init__(self): self._data = range(10) def __getitem__(self, key): return self._data.__getitem__(key) I can use it like this: w = wrap() print w[2] # yields \"2\" I thought I could optimize and get rid of one function call by changing to this: class wrap

Simple way to get wrapper class type in Java

ⅰ亾dé卋堺 提交于 2019-11-26 09:49:46
问题 I have a piece of code where I need to pass the class of a field in a method. Because of the mechanics of my code I can only handle reference objects and not primitives. I want an easy way of determining if a Field \'s type is primitive and swap it with the appropriate wrapper class. So in code what I do so far is something like this: Field f = getTheField(); // Dummy method that returns my Field Class<?> c = f.getType(); if (c == int.class) { c = Integer.class; } else if (c == float.class) {

What is the radix parameter in Java, and how does it work?

▼魔方 西西 提交于 2019-11-26 09:42:31
问题 I understand that radix for the function Integer.parseInt() is the base to convert the string into. Shouldn\'t 11 base 10 converted with a radix/base 16 be a B instead of 17 ? The following code prints 17 according to the textbook: public class Test { public static void main(String[] args) { System.out.println( Integer.parseInt(\"11\", 16) ); } } 回答1: When you perform the ParseInt operation with the radix, the 11 base 16 is parsed as 17, which is a simple value. It is then printed as radix 10

Creating simple c++.net wrapper. Step-by-step

假装没事ソ 提交于 2019-11-26 09:18:56
问题 I\'ve a c++ project. I admit that I\'m a complete ZERO in c++. But still I need to write a c++.net wrapper so I could work with an unmanaged c++ library using it. So what I have: 1) unmanaged project\'s header files. 2) unmanaged project\'s libraries (.dll\'s and .lib\'s) 3) an empty C++.NET project which I plan to use as a wrapper for my c# application How can I start? I don\'t even know how to set a reference to an unmanaged library. S.O.S. 回答1: http://www.codeproject.com/KB/mcpp

When using wrapper, how to preserve class and method name for Log4Net to log?

半世苍凉 提交于 2019-11-26 08:37:28
问题 I need a Log4net wrapper - to be exposed to a number of different components in a large app. I obviously want to retain the class and method name when logging but I would keep away of passing down type etc to my wrapper. I had a look at this question which is very similar to mine, but it didn\'t help. I\'ve seen it done in this other question with smt like the following: MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); this.log.Debug(methodBase.Name + \" : \" + message); This

When to use wrapper class and primitive type

怎甘沉沦 提交于 2019-11-26 07:47:23
When I should go for wrapper class over primitive types? Or On what circumstance I should choose between wrapper / Primitive types? pstanton Others have mentioned that certain constructs such as Collections require objects and that objects have more overhead than their primitive counterparts (memory & boxing). Another consideration is: It can be handy to initialize Objects to null or send null parameters into a method/constructor to indicate state or function. This can't be done with primitives. Many programmers initialize numbers to 0 (default) or -1 to signify this, but depending on the

How != and == operators work on Integers in Java? [duplicate]

笑着哭i 提交于 2019-11-26 04:27:13
问题 This question already has answers here : Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java? (6 answers) Closed 3 years ago . The following code seemed really confusing to me since it provided two different outputs.The code was tested on jdk 1.7. public class NotEq { public static void main(String[] args) { ver1(); System.out.println(); ver2(); } public static void ver1() { Integer a = 128; Integer b = 128; if (a == b) { System.out.println(\"Equal Object\"); }

How to decorate all functions of a class without typing it over and over for each method? [duplicate]

旧街凉风 提交于 2019-11-26 04:16:35
问题 This question already has answers here : Attaching a decorator to all functions within a class (7 answers) Closed 2 years ago . Lets say my class has many methods, and I want to apply my decorator on each one of them, later when I add new methods, I want the same decorator to be applied, but I dont want to write @mydecorator above the method declaration all the time? If I look into __call__ is that the right way to go? IMPORTANT: the example below appears to be solving a different problem

How to use C++ in Go

折月煮酒 提交于 2019-11-26 03:47:08
问题 In the new Go language, how do I call C++ code? In other words, how can I wrap my C++ classes and use them in Go? 回答1: Update: I've succeeded in linking a small test C++ class with Go If you wrap you C++ code with a C interface you should be able to call your library with cgo (see the example of gmp in $GOROOT/misc/cgo/gmp ). I'm not sure if the idea of a class in C++ is really expressible in Go, as it doesn't have inheritance. Here's an example: I have a C++ class defined as: // foo.hpp

Using == operator in Java to compare wrapper objects

落爺英雄遲暮 提交于 2019-11-26 02:35:48
问题 I\'m reading SCJP Java 6 by Kathy Sierra and Bert Bates and this book is confusing me so much. On page 245 they state that the following code below. Integer i1 = 1000; Integer i2 = 1000; if(i1 != i2) System.out.println(\"different objects\"); //Prints output different objects Then on the very next page they have the following code Integer i3 = 10; Integer i4 = 10; if(i3 == i4) System.out.println(\"same objects\"); //Prints output same objects I\'m so confused! When I try this out on my own it