0
3

[–] SithEmpire 0 points 3 points (+3|-0) ago 

Java does something similar to reduce object creation when the value of a primitive type such as 'int' is assigned to a variable of its class equivalent 'Integer'; the runtime always creates a set of reusable (and hopefully immutable!) Integer objects for small values.

E.g.

int a = 5;                     // Ordinary primitive
Integer b = new Integer(5);    // New object created explicitly
Integer c = 5;                 // Prepared object for '5' is recalled/reused
Integer d = 555;               // New object created automatically

I didn't know that Python did that, but I'm definitely passing that article on to a friend who's learning it (first programming language!). It's probably common to most runtimes which perform some sort of automatic conversion/creation.

0
2

[–] Phen 0 points 2 points (+2|-0) ago 

(and hopefully immutable!)

Nothing is immutable with malicious reflection! Just nudge the prival final value field to sow great confusion. http://www.docjar.com/html/api/java/lang/Integer.java.html

Even more fun, you can even change the toString() output of ints without trouble. The chars used for toString()ing are stored in an array (digits field) which can be updated, so that you can have int 0 output as "7", for example, and then 10 as "17".

I can dig up the code for this if you want to see, but it's also fun to figure out on one's own.

0
0

[–] SithEmpire 0 points 0 points (+0|-0) ago 

Mm, I do already use Field.setAccessible, Field.get, Field.set to mess with some library classes (mostly for hooking stuff which doesn't provide enough built-in ways to hook it), which should also be able to change the value of a private final field given its name.

I didn't consider methods before, but now that you mention it I really want a way to get/set a method implementation directly. The best would be if it worked with lambdas, though that will never work unless/until a method signature can be described by referring to an existing method rather than needing a separate interface for it (that being a separate feature I already knew I wanted). The dream would be e.g.:

Integer.class.getMethod("toString").set( obj -> "Object representing the number " + obj.intValue() );

0
0

[–] Atarian 0 points 0 points (+0|-0) ago 

Forgive my ignorance, but what is the benefit of doing it this way? Is it for speed?

0
0

[–] svipbo [S] 0 points 0 points (+0|-0) ago 

I honestly don't know but I am guessing it is because integers are Python objects, so when an integer constant appears in the source code the interpreter has to get a data structure representing this integer. This would allow methods to be called on the object.

0
1

[–] Atarian 0 points 1 points (+1|-0) ago  (edited ago)

It does seem a little crazy to me. Fetching a data structure when the processor can deal with an int using a single instruction and memory location seems just plain bloat.

Saying that, I did all my assembly programming when there was no such thing as an L2 cache.

0
0

[–] littul_kitton 0 points 0 points (+0|-0) ago 

Yes. It greatly reduces the footprint in the cache and TLB.

0
0

[–] Atarian 0 points 0 points (+0|-0) ago 

Seems like the sort of thing that could cause chaos with a bad memory write. The chances of that happening are very slim though, I guess.