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

"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:

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

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