Can you create actual classes and how?

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:

2 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.

Yeah true what is true OOP and false OOP if it does the same thing why should we consider them differently ?

Doing :

ClassTable = {
    new = function(self,x,y)
        local object = setmetatable({},{__index = self})
        object.x = x
        object.y = 2 * x
        return object
    end
}

newObject = ClassTable:new(2)

is essentially the same thing as doing :

class myClass():
    def __init__(self,x,y):
         self.x = x
         self.y = 2 * x
myClassObject = myClass(2)

I wouldn’t put a difference on them, what I’m referring to is saying Lua doesn’t have classes while saying Python and/or JS do; all 3 have support for it. OOP is the broader scope of the subject. If you can have a mutable state that can be used by functions, you ideally have OOP. I’m debating against what was mentioned here Lua supposedly can’t have classes while Python can.

1 Like

Oh okay I (think I) get it, you mean that everything that could be used as OOP is OOP

Okay, in a way, but again I wasn’t referring to OOP in general. OOP is very broad and can be applied almost anywhere; this just dragged on and away from what I was first discussing, which was:

Although lua does have built-in classes. It just doesn’t let you create classes.

Actually I kinda don’t get it are metatables classes ?

Metatables aren’t really classes, they’re a way that you can create custom classes.

Take a look at this article:
http://lua-users.org/wiki/MetamethodsTutorial

If you want an example of how metamethods can be used to “create” classes, I used it in my party system. Here’s the constructor for a “Party object”

function party.new(player)
	local self 			= setmetatable({}, {
		__index = party,
		__tostring = function(self)
			return self.leader.Name .. "'s Party. Members: " .. self:GetMemberCount()
		end;
		
	});
	self.leader 		= player
	self.members 		= {[player] = true}
	
	AddParty(self) -- adds this party to a collection of parties so we can loop through them or something
	
	return self
end

So first of all, all tables can have metatables.

Metatables are tables of customized methods which control what should happen when you try an unknown operation on the table.
For example: table + 1 invokes the __add method in its metatable, and table() invokes the __call method.

For classes, whenever you index a table via table[index] or table.index, you invoke the __index method. If the value assigned to this method is a table, then that table is indexed if the original table does not have it.

This is incorrect. Only tables explicitly given a metatable via setmetatable have a metatable.

2 Likes

Metatables are basically classes right or are they an interpreted way to do it ?

Does it necessarily have to be a class? You could always use Tags via CollectionService instead.