473,386 Members | 1,973 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,386 software developers and data experts.

Tapping into the access of an int instance

Hi,

Does anyone know how to interrupt the lookup of an integer value? I
know I need to subclass int, since builtin types can't be altered
directly...

Below is how far I've come... What I want is to tap into the access of
instance i's value 1...
>>class Int(int):
def __init__(self, *a, **k):
super(Int, self).__init__(self, *a, **k)

>>i = Int(1)
i
1

Regards,
Tor Erik
Sep 20 '07 #1
5 1007
On Sep 20, 1:21 pm, "Tor Erik Sønvisen" <toreri...@gmail.comwrote:
Hi,

Does anyone know how to interrupt the lookup of an integer value? I
know I need to subclass int, since builtin types can't be altered
directly...

Below is how far I've come... What I want is to tap into the access of
instance i's value 1...
>class Int(int):

def __init__(self, *a, **k):
super(Int, self).__init__(self, *a, **k)
>i = Int(1)
i

1

Regards,
Tor Erik
While I'm not *totally* sure what you are asking, you can access the
first parameter you passed in __init__ via args[0].

Sep 20 '07 #2
"Tor Erik Sønvisen" <to*******@gmail.comwrites:
Does anyone know how to interrupt the lookup of an integer value?
AFAIK, int values *aren't* "looked up", in my understanding of that
term.

Can you explain what events you want to intercept?
Below is how far I've come... What I want is to tap into the access
of instance i's value 1...
In many senses "instance i" *is* "value 1".

What "access" do you want to intercept? Can you show some code
examples of things that you would expect to trigger this "access"?
Also, some similar events involving the instance that would *not*
trigger the same thing.

--
\ "The generation of random numbers is too important to be left |
`\ to chance." -- Robert R. Coveyou |
_o__) |
Ben Finney
Sep 20 '07 #3
Tor Erik Sønvisen wrote:
Hi,

Does anyone know how to interrupt the lookup of an integer value? I
know I need to subclass int, since builtin types can't be altered
directly...

Below is how far I've come... What I want is to tap into the access of
instance i's value 1...
>>>class Int(int):
def __init__(self, *a, **k):
super(Int, self).__init__(self, *a, **k)

>>>i = Int(1)
i
1

Regards,
Tor Erik
Perhaps you could elaborate on what you want to do with that value once
you "tap" it.
Sep 21 '07 #4
Tor Erik Sønvisen wrote:
Does anyone know how to interrupt the lookup of an integer value? I
know I need to subclass int, since builtin types can't be altered
directly...

Below is how far I've come... What I want is to tap into the access of
instance i's value 1...
>>>class Int(int):
def __init__(self, *a, **k):
super(Int, self).__init__(self, *a, **k)

>>>i = Int(1)
i
1
You may be looking for __new__() which is invoked before an object is
created. This is particular useful for immutables like int.
>>lookup = {}
class Int(int):
.... def __new__(cls, value, *more):
.... if not more:
.... try:
.... return lookup[value]
.... except KeyError:
.... pass
.... return int.__new__(cls, value, *more)
....
>>lookup["answer"] = Int(42)
Int("answer")
42
>>type(_)
<class '__main__.Int'>

Peter
Sep 21 '07 #5
>>>>ch************@gmail.com (CM) wrote:
>CMOn Sep 20, 1:21 pm, "Tor Erik Sønvisen" <toreri...@gmail.comwrote:
>>Hi,

Does anyone know how to interrupt the lookup of an integer value? I
know I need to subclass int, since builtin types can't be altered
directly...

Below is how far I've come... What I want is to tap into the access of
instance i's value 1...

class Int(int):

def __init__(self, *a, **k):
super(Int, self).__init__(self, *a, **k)

i = Int(1)
i

1

Regards,
Tor Erik
>CMWhile I'm not *totally* sure what you are asking, you can access the
CMfirst parameter you passed in __init__ via args[0].
What args do you mean?
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Oct 2 '07 #6

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

Similar topics

166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
1
by: Alex | last post by:
Acc97 on a subform I have a series of Combo box's which because of my inability to think ahead! I have had to use a field on the last one which I did not intend to use, and bound to it....
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
0
by: Ahmedhussain | last post by:
Hi everyone, I am totally new to IIS. I somehow configured IIS and now whenever I try to add a website, on testing it gives me the error of authorization, i.e. "Cannot verify access to path :...
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: 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:
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.