Can you create actual classes and how?

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

and then you would do like

myClassObject = myClass(2)

15 Likes

ModuleScripts, that’s it.

You create functions and variables within it, so yeah.


Example:

local myModule = {}

myModuleVariable = 0
myModule.String = "Hi"

function myModule:MyFunc()
    -- custom stuff
end

return myModule
2 Likes

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:

https://developer.roblox.com/articles/Object-Oriented-Programming

I hope you find your solution!

8 Likes

I’m very used to python and I don’t get how you DRY your code whilst not creating classes.

3 Likes

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)
27 Likes

Yes, it can be hard initially to find how to not have to reuse code a lot, but please give the articles I listed a peek. Thanks :slight_smile:

Also, like @Operatik said, ModuleScripts are great too.

2 Likes

Lua doesn’t have a “class” construct the way that python or most other scripting languages do. You have to make them through tables and metatables.

If you’re just concerned about DRY, then there are several modules reduce a lot of the boilerplate for you. A favorite of mine is MiddleClass:

5 Likes

I originally though python and lua were similar but how wrong was I …

5 Likes

You don’t need modules at all to have classes.

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.

5 Likes

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!

2 Likes

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.

1 Like

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.

2 Likes

So to confirm, a language like C# has actual classes, but Python just has syntax sugar to make it easier to create your own?

I just want to clarify my understanding, thanks.

1 Like

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.

2 Likes

Alright, thank you!

This 50 char thing makes me think the Staff want us to stop making statements like this :thinking:

4 Likes

So you’re basically saying python OOP isn’t actual OOP because it’s interpreted or do I just not understand ?

1 Like

Python classes seem to be implemented in a similar way to JS, too.
image

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.

2 Likes

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.

1 Like

The point is python do claim to be OOP which seems like it is fake OOP, like lua ! that’s my understanding

1 Like

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.