Tapping into the access of an int instance

  • Thread starter =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?=
  • Start date
?

=?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?=

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...
def __init__(self, *a, **k):
super(Int, self).__init__(self, *a, **k)

1

Regards,
Tor Erik
 
C

chris.monsanto

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...


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


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].
 
B

Ben Finney

Tor Erik Sønvisen said:
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.
 
J

James Stroud

Tor said:
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...

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


1

Regards,
Tor Erik

Perhaps you could elaborate on what you want to do with that value once
you "tap" it.
 
P

Peter Otten

Tor said:
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...

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


1

You may be looking for __new__() which is invoked before an object is
created. This is particular useful for immutables like 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
 
P

Piet van Oostrum

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

What args do you mean?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top