Extending long

Martin v. Loewis martin at v.loewis.de
Wed Jun 19 17:00:29 EDT 2002


estama at dblab.ntua.gr (Freeman Stopjohn) writes:

> class bitnum(long):
>     def getbit(self,bitn):
>         if self & (1 << bitn) >0:
>             return 1
>         else:
>             return 0
>     def setbit(self,bitn):
>         self=self ^ ( 1 << bitn )
> -----
> 
>  The getbit method works ok.
>  The setbit doesn't (it doesn't change the number).
> 
>  What am i doing wrong?

You are assigning to self. self is a parameter of setbit, just like
bitn. Assignments to formal parameters have no effect.

What you want to do cannot be done; long objects are immutable. I
recommend you implement a wrapper, rather than inheriting.

Regards,
Martin




More information about the Python-list mailing list