Sorry, I know this may be an odd questions, but I made this basic JavaScript program, and I'm not exactly sure what I should look up to know how to translate it to a python program with the same functionality. I know how the code works, but I don't know what the technical term for it is so I am struggling to find out how to do it in python. Any help would be greatly appreciated. Thanks!
var myArray = [];
var myCache = {
add: function a(x){
myArray.shift();
myArray.push(x);
},
access: function b(z){
var zLocation = myArray.indexOf(z);
var temp1 = myArray.slice(0,zLocation);
var temp2 = myArray.slice(zLocation+1, myArray.length);
temp1 = temp1.concat(temp2);
temp1.push(z);
myArray = temp1;
},
print: function c(){
console.log("Current array is: " + myArray);
},
size: function d(y){
yArray.length = y;
}
};
myCache.add(7);
I don't know how to add the add, access, print, and size functionality to something I create in Python. Thanks!
myCache, an object, with functions as values. You can make a class in python with methods that do this. Read about classes and methods and it should become clear how to do this.