471,326 Members | 2,144 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 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 959
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

166 posts views Thread by Graham | last post: by
15 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
reply views Thread by rosydwin | last post: by

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.