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

  • Home
  • SEARCH
  • 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 951563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:45:18+00:00 2026-05-15T23:45:18+00:00

In PHP one can create a reference variable, so that two named variables can

  • 0

In PHP one can create a reference variable, so that two named variables can look at the same value:

$a = 1;
$b =& $a;
echo $a; // 1
echo $b; // 1
$b = 2;
echo $a; // 2

I’m looking to achieve something similar in Python. Specifically, I want to create a reference to an object’s property, eg:

class Foo(object):
  @property
  def bar(self): return some_calculated_value

foo_instance = Foo()
ref = foo_instance.bar 
# so that 'ref' is referencing the bar property on foo, calculated when used.

Is this possible?

  • 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-05-15T23:45:19+00:00Added an answer on May 15, 2026 at 11:45 pm

    There is some more magic that can be done in Python (not that I would recommend it and it will require digging on your part ;-), but using a closure may be sufficient for your needs:

    get_x = lambda: foo_instance.bar
    get_x() # yahoo!
    

    Edit, for those wanting “update support”, it’s all about the closures:

    def wrap_prop (obj, pname):
      def _access(*v):
        if not len(v):
          return getattr(obj, pname)
        else
          setattr(obj, pname, v[0])
      return _access
    
    class z (object):
      pass
    
    access = wrap_prop(z(), "bar")
    access(20)
    access() # yahoo! \o/
    

    Without stepping outside the bounds of normally (for me 🙂 accepted Python, this could also be written to return an object with forwarding/proxy property, imagine:

    access = wrap_prop(z(), "bar")
    access.val = 20
    access.val # yahoo \o/
    

    Some interesting links:

    • http://adam.gomaa.us/blog/2008/aug/11/the-python-property-builtin/
    • http://code.activestate.com/recipes/576787-alias-class-attributes/
    • http://www.faqs.org/docs/diveintopython/dialect_locals.html
    • http://docs.python.org/library/functions.html (see “property”)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP, one can do foreach($array as $key=>$value){//codes . Is it possible to create
How can I post a php variable from one file to an other? I
Can any one say how we can create folder using php. I tried to
How to install the oauth in php. Can any one provide me the full
okey so here is my structure. one.php two.php three.php one.php includes both two.php and
my website is based off of only one php index file that changes the
I have three files in a folder 'test' one.php two.php print.html And i have
Iam working on Ubuntu.(Linux) I want to redirect from the page one.php to two.php,
I can't figure out why this script won't work unless i create a reference
We've used php to build up an interface that the client can upload svg

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.