Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Overloading the ">>" operator in Python 2.2.1

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


andy.elvey at paradise

Sep 12, 2002, 1:22 AM

Post #1 of 3 (129 views)
Permalink
Overloading the ">>" operator in Python 2.2.1

Hi all .
I'm using Python 2.2.1 under Win98. I'm doing some experimenting with
parsing, and as part of that, I'm trying to overload the right-shift
operator (>>) so that I can use it as follows (testing against a sequence
that I read in) -
a >> b would mean "a is followed by b" .
So, you could have a function like seqtest("string_to_test" ,
"foo">>"bar" ) , and that function would return true or false, depending on
whether foo was followed by bar.
( I know it's possible to use regexes for this, but being able to create
a sequence-testing operator like this would be useful .)
Many thanks in advance ......


tjreedy at udel

Sep 12, 2002, 5:57 AM

Post #2 of 3 (121 views)
Permalink
Overloading the ">>" operator in Python 2.2.1 [In reply to]

"Andy Elvey" <andy.elvey [at] paradise> wrote in message
news:fhYf9.1418$b5.128061 [at] news02
> Hi all .
> I'm using Python 2.2.1 under Win98. I'm doing some experimenting
with
> parsing, and as part of that, I'm trying to overload the right-shift
> operator (>>) so that I can use it as follows (testing against a
sequence
> that I read in) -
> a >> b would mean "a is followed by b" .
> So, you could have a function like seqtest("string_to_test" ,
> "foo">>"bar" ) , and that function would return true or false,
depending on
> whether foo was followed by bar.
> ( I know it's possible to use regexes for this, but being able to
create
> a sequence-testing operator like this would be useful .)
> Many thanks in advance ......

You did not explicitly ask a question, but here's a possible answer
anyway:

>>> class wierd(str):
... def __rshift__(self,other):
... print self, 'followed by', other
...
>>> foo = wierd('foo')
>>> bar = wierd('bar')
>>> foo>>bar
foo followed by bar

You cannot add attributes to built-in types. You can only subclass
them (in 2.2+).

Terry J. Reedy


exarkun at meson

Sep 12, 2002, 6:06 AM

Post #3 of 3 (121 views)
Permalink
Overloading the ">>" operator in Python 2.2.1 [In reply to]

--Q68bSM7Ycu6FN28Q
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Thu, Sep 12, 2002 at 08:22:07PM +1200, Andy Elvey wrote:
> Hi all .
> I'm using Python 2.2.1 under Win98. I'm doing some experimenting with
> parsing, and as part of that, I'm trying to overload the right-shift
> operator (>>) so that I can use it as follows (testing against a sequence
> that I read in) -
> a >> b would mean "a is followed by b" .
> So, you could have a function like seqtest("string_to_test" ,
> "foo">>"bar" ) , and that function would return true or false, depending =
on
> whether foo was followed by bar.
> ( I know it's possible to use regexes for this, but being able to crea=
te
> a sequence-testing operator like this would be useful .)
> Many thanks in advance ......
>=20

>>> class Foo:
=2E.. def __rshift__(self, other):
=2E.. print 'rshift'
=2E.. def __lshift__(self, other):
=2E.. print 'lshift'
=2E..
>>> f =3D Foo()
>>> f >> 'hello'
rshift
>>> f << 'world'
lshift


Also:

>>> dir(42)
[.'__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
'__floordiv__', '__getattribute__', '__hash__', '__hex__', '__init__',
'__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__',
'__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__',
'__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__',
'__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__',
'__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',
'__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__',
'__truediv__', '__xor__']

Jp

--=20
It is practically impossible to teach good programming style to
students that have had prior exposure to BASIC: as potential
programmers they are mentally mutilated beyond hope of
regeneration. -- Dijkstra
--
9:00am up 114 days, 9:53, 4 users, load average: 0.02, 0.01, 0.00

--Q68bSM7Ycu6FN28Q
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9gJFGedcO2BJA+4YRApWxAKCPdrsPxvdIKeJuttJVUJ8fv26iggCgxTTW
2v9Yg7iV6AUqeyFHbJsS4uE=
=npRf
-----END PGP SIGNATURE-----

--Q68bSM7Ycu6FN28Q--

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.