473,378 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

switching an instance variable between a property and a normal value

I'd like to be able to have an instance variable that can sometimes be
accessed as a property, and sometimes as a regular value, e.g. something
like:

py> class C(object):
.... def usevalue(self, x):
.... self.x = x
.... def usefunc(self, func, *args, **kwds):
.... self._func, self._args, self._kwds = func, args, kwds
.... self.x = C._x
.... def _get(self):
.... return self._func(*self._args, **self._kwds)
.... _x = property(_get)
....
py> c = C()
py> c.usevalue(4)
py> c.x
4
py> c.usefunc(list)
py> c.x # I'd like this to print []
<property object at 0x04166DA0>

Of course, the code above doesn't do what I want because C._x is a
property object, so the assignment to self.x in usefunc just adds
another name for that property object. If I use self._x (or
getattr(self, '_x'), etc.) then self._func only gets called that one time:

py> class C(object):
.... def usevalue(self, x):
.... self.x = x
.... def usefunc(self, func, *args, **kwds):
.... self._func, self._args, self._kwds = func, args, kwds
.... self.x = self._x
.... def _get(self):
.... return self._func(*self._args, **self._kwds)
.... _x = property(_get)
....
py> c = C()
py> c.usefunc(list)
py> c.x is c.x # I'd like this to be False
True

Is there any way to get the kind of behavior I'm looking for? That is,
is there any way to make self.x use the property magic only some of the
time?

Steve

P.S. Yes, I know I could make both access paths run through the
property magic, with code that looks something like:

py> class C(object):
.... _undefined = object
.... def __init__(self):
.... self._value, self._func = C._undefined, C._undefined
.... def usevalue(self, x):
.... self._value = x
.... self._func = C._undefined
.... def usefunc(self, func, *args, **kwds):
.... self._func, self._args, self._kwds = func, args, kwds
.... self._value = C._undefined
.... def _get(self):
.... if self._value is not C._undefined:
.... return self._value
.... if self._func is not C._undefined:
.... return self._func(*self._args, **self._kwds)
.... raise AttributeError('x')
.... x = property(_get)
....
py> c = C()
py> c.usevalue(4)
py> c.x
4
py> c.usefunc(list)
py> c.x is c.x
False

This code is kinda complicated though because I have to make sure that
only one of self._func and self._value is defined at any given time.
Jul 18 '05 #1
1 1114
Steven Bethard wrote:
I'd like to be able to have an instance variable that can sometimes be
accessed as a property, and sometimes as a regular value, e.g. something
like:


If you want the behaviour to be switchable per-instance, you have to go the
route of always running through the property machinery, since property() creates
a descriptor on the class, not the instance.

On the other hand, if you want to switch the behaviour of every instance, then
making usevalue and usefunc class methods may give you some mileage.

I'm rather curious about your use case, though. . .

Here's a different way of using the property machinery, too:

Py> class C(object):
.... def __init__(self):
.... self._use_val = None
.... def useval(self, x):
.... self._val = x
.... self._use_val = True
.... def usefunc(self, func, *args, **kwds):
.... self._func, self._args, self._kwds = (func, args, kwds)
.... def _get(self):
.... use_val = self._use_val
.... if use_val is None:
.... raise AttributeError('x')
.... if use_val:
.... return self._val
.... else:
.... return self._func(*self._args, **self._kwds)
.... x = property(_get)
....
Py> c = C()
Py> c.useval(4)
Py> c.x
4
Py> c.usefunc(list)
Py> c.x
[]

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
3
by: DraguVaso | last post by:
Hi, I'm having the following situation: - A class clsFournisseur with public property's which raise a MyPropertyChanged-event in the Set-method for each Property. Public Event NomChanged As...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
22
by: WXS | last post by:
Sometimes a method in a class requires the use of class instance variables/fields that will not be used outside of the method itself. Currently this means you must create a instance field in the...
4
by: Angel Mateos | last post by:
Is posible something like this? public Class Class1 property readonly InstanceName get return me.InstanceName endget end property end class
4
by: simon | last post by:
hello. relatively new to vb.net, i'm using VS 2003 and .net 2.0 i have a web app that i'm i have a user control that displays a simple 1 row table as the header of the page. the user control...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
45
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.