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 8653049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:26:59+00:00 2026-06-12T14:26:59+00:00

When you print certain types in lua (such as functions and tables), you get

  • 0

When you print certain types in lua (such as functions and tables), you get the name of the type and an address, as below:

> tab = {}
> print(tab)
table: 0xaddress

I have created a simple class as below, and I would like to override the __tostring method similarly. How do I get the address of the object that I want to print?

Here’s my class. I would like print(pair) to print out Pair: 0xaddress. Obviously this is a trivial example, but the concept is useful:

Pair = {}
Pair.__index = Pair

function Pair.create()
 local p = {}
 setmetatable(p, Pair)
 p.x = 0
 p.y = 0
 return p
end

function Pair:getx()
 return self.x
end

function Pair:gety()
 return self.y
end

function Pair:sety(iny)
 self.y=iny   
end

function Pair:setx(inx)
 self.x=inx
end
  • 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-12T14:27:00+00:00Added an answer on June 12, 2026 at 2:27 pm

    Here’s a hokey way to do it:

    Pair.__tostringx = function (p)
        Pair.__tostring = nil    
        local s = "Pair " .. tostring(p)                                                                                                                                                                                                    
        Pair.__tostring = Pair.__tostringx
        return s
    end
    Pair.__tostring = Pair.__tostringx    
    
    > print(p)
    Pair table: 0x7fe469c1f900
    

    You can do more string manipulation inside Pair.__tostringx to get the format you want, e.g., to remove “table”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to print out SQL statements in NHibernate log file without certain blob-type
I want to write an app for a certain type of printer to print
I'm passing an object of a certain type to an array. When I print
One attribute in a table became corrupted after a certain point in a table
Given a table where the first column is seconds past a certain reference point
In NumPy functions, there are often initial lines that do checking of variable types,
We've developed a security product which identifies certain types of unauthorized traffic on a
I have a function that takes an object of a certain type, and a
I'm trying to block certain types of characters from being inserted into my Edit
I'm using a Python script in EventGhost to match certain file types in a

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.