Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7969643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:16:09+00:00 2026-06-04T07:16:09+00:00

I did not know what to name this question as I don’t understand enough

  • 0

I did not know what to name this question as I don’t understand enough of what is going on.(Feel free to edit)

Consider the code below.

function object:new()
    o = o or {
        x = 0
    }
    setmetatable(o, self)
    self.__index = self
    self.y = 0

return o
end

table = object:new()

What are the differences between the variables (o.x and self.y) later on?

If I print_r the variable table, only the x is returned. However, both table.x and table.y can be accessed. This makes me realise that there is a difference between the two.

Could someone explain what the difference is and what reasons there are for putting variable in differant places?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:16:10+00:00Added an answer on June 4, 2026 at 7:16 am

    What are the differences between the variables (o.x and self.y) later on?

    You have two tables here, object and o.

    Inside of object:new, self refers to the table object. So self.y is a field in the table object.

    o is the new table you create in each call to object:new. o.x is a field in the table o.

    That o table only has one entry: o["x"], so when you iterate over the entries in the table (as print_r does), that’s all you’re going to see.

    So why does o.y give you a value? Because you set the table object as o‘s metatable and that metatable has it’s __index field set, so when an index attempt fails on o, Lua will try again via o‘s metatable (if it has __index set).

    A little code will probably make this clearer:

    o = { x = 33 }
    
    print(o.x, o.y) --> 33 nil
    
    -- let's give o a metatable
    mt = { y = 20 }
    setmetatable(o, mt)
    
    -- we've given o a metatable, but that metatable doesn't have an __index member set
    print(o.y) --> nil
    
    -- metatable `handlers` are normally functions that handle events
    -- here we create a handler for indexing a table if the key doesn't exist in the table:
    mt.__index = function(t,k)
      print("An attempt was made to index table", t, "with the key", k)
      return 5150
    end
    
    -- we've given o's metatable a handler for indexing which always returns 5150
    print(o.x) --> 33
    print(o.y) --> 5150
    print(o.z) --> 5150
    print(o.donut) --> 5150
    
    -- note that Lua has a "rawget" function, which bypasses the metatable mechanics
    print(rawget(o,'x')) --> 33, we get a value, because o actually contains "x"
    print(rawget(o,'y')) --> nil, back to nil, because the metatable is being ignored
    
    -- the __index metatable handler is special. Instead of providing a function
    -- to handle missing key events, you can give it a table. If an index attempt fails,
    -- it will try again in the __index table
    mt.__index = mt -- we could have used a third table here, but mt has the y entry we want
    
    -- we've changed the metatable handler, so now we'll get 777
    print(o.y) --> 777
    
    -- changes to the metatable are reflected in any object using it as metatable
    mt.__index.y = 999
    print(o.y) --> 999
    
    -- now we give `o` it's OWN "y" entry, so the metatable handler will no longer be invoked
    o.y = 2112
    
    print(o.y) --> 2112
    print(rawget(o, 'y')) --> o really owns this "y" entry
    print(mt.__index.y) --> 999, the metatable was untouched by our write to o.y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is a recurrent/classical topic but I did not found anything that
I did not find an answer to this question. I have a VOIP application.
I did see this question but not much on .NET... I want to write
I know this is not a straight up question, so if you need me
EDIT: This question title originally was: How does Doctrine know last inserted id in
I had problem even in choosing a title for this question. Please feel free
I understand my question is a bit vague but I don't know how else
I did not find any suitable questions answered yet, so I'd like to know
I did search the forum and did not find a similar question. I'm looking
I asked a similar question last week but did not get an answer that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.