In python and js you can create classes and add methods or variable to them I wondered if you could do that here, and how would you.
For exemple you would do this in python:
class myClass():
def __init__(self,x,y):
self.x = x
self.y = 2 * x
In short, no, you cannot create classes. But, you can still have an OOP workflow. There are a couple of articles that really helped when I started to learn about OOP:
classes are essentially created using tables instead, especially through metatables:
“Pythonic” variant:
local ClassTable = {
new = function(self, x, y)
local object = setmetatable({}, {__index = self})
object.x = x
object.y = 2 * x
return object
end
}
local newObject = ClassTable:new(2)
Idiomatic variant:
local ClassTable = {}
ClassTable.__index = ClassTable
function ClassTable.new(x, y)
local self = {
x = x,
y = 2 * x
}
setmetatable(self, ClassTable)
return self
end
local newObject = ClassTable.new(2)
If we’re going by the definition of Python and (I think) JS classes, we certainly can create them.
Python doesn’t really have classes either, it’s just syntactic sugar from what it seems, so it’s probably closer to what you’d do in Lua under the hood.
Edit: Seems that JS and Python (at least the distributions I’m running) handle classes via constructor and dictionaries, so much in the same way Lua would except they have the sugar for it.
@ OP
Ideally you implement classes by having a constructor function (that’s what Python and JS do afaik) which returns an object (a table in our case) which somehow has a reference to its own methods. This can be done in any way from using/attaching metatables to just setting the references manually on the table, and each way has its own pros/cons.
The syntax is VERY different and things like this are not similar at all. But many principles are the same, and I believe that with diligent effort you can master Lua! Best of wishes in your endeavours!
Would it matter how classes are implemented under the hood in python? Language constructs have all to do with how you write code in a language. If I can’t declare a class using the language’s built-in syntax, then I can say that the class construct doesn’t exist.
That’s at eye level and if you’re willing to disregard the fact that both languages have the same implementation, it’s just that one has the sugar for it.
Yep. Compiled languages evaluate the class structure before runtime often times and just produce the raw memory structure, and often don’t even have to look up methods when you call them.
Python classes seem to be implemented in a similar way to JS, too.
Most interpreted languages seem to have a base “object” type (called class sometimes, but it’s nice to make a distinction). Python, JavaScript and Lua don’t have classes in the way C# and such would. Each actually have their own form of “metatables” to use a common term. JavaScript uses prototypes for method resolution and overloading/behavior, Python uses __method__s for overloading and behavior, and Lua uses metatables for the same.
I think it’s unfair to say “you can’t create classes in Lua” and then in the same thought say “but you can do so in Python/JS/other scripting languages”, and it’s repeated a lot to the point where it’s misleading. The 3 languages used in the example have OOP support, and it’s just implemented slightly differently, with two of them having a wrapper keyword for it.
The difference between interpreted classes and compiled language classes are that the latter are evaluated at compile time, and actual structures, as opposed to wrappers, are created.
Relevant to Python classes being wrappers:
Similar can be observed with JS classes and Lua implementations of similar behavior. Additionally, you can look to MoonScript’s class keyword which generates the equivalent Lua OOP code for an example of what the Python and JavaScript class keywords might do, under the hood or right in place.
The one I recommend is my own Nexus Instance library. The advantage it has over the other Lua libraries is that it is targetted to Roblox by having the .Changed signal built in. Due to how Lua’s metamethods work, this implementation can be painful to do on your own. It also uses NexusInstance.new() instead of NexusInstance:new() to be consistent with Roblox’s syntax.
I don’t see how this makes any sense considering. How does Python have support for OOP where Lua and JavaScript don’t? Classes in Lua aren’t just “functionally close enough to Python”, Python’s “classes” are implemented using a similar approach to Lua’s.