473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Inheriting from int or long

I started creating a simple "bits" class, intended to act like a array
of bits. This was my initial idea, basically just overriding the
string representation to display the bitmask (so far):

class bits(long):
def __str__ (self):
s = ''
if self == 0L:
s += '-'
else:
x = self
while x >= 1:
if x & 1L: s = '#' + s
else: s = '-' + s
x >>= 1
return s

My thought was this will act like a Long, and so should act like an
array of bits with arbitrary precision, supporting the standard
bitwise operations, along with others I may add.
>>import bits
b = bits.bits(32)
b
32L
>>print b
#-----
>>b = bits.bits(35)
print b
#---##
>>b = bits.bits(36)
print b
#--#--
>>b <<= 1
print b
72

So obviously when I attempt a bitwise operation (ex. b <<= 1), b is
being assigned the result of b << 1, which is a Long.
>>b = bits.bits(36)
type(b)
<class 'bits.bits'>
>>type (b << 1)
<type 'long'>

Is there any way around this without reimplementing all of the bitwise
operators? It's not the biggest deal to reimplement the operators, but
it kind of defeats the benefit of inheriting from an integer type.

If there isn't a way around this, then I am curious in what situations
it would be beneficial to inherit from an int or long. This issue
seems more related to numeric types, since they are so centered around
operations that involve assignment, as opposed to, say, a List, which
has some operators (ex. +=), but does not depend on them (ex. use
append() instead).

Oct 2 '07 #1
2 1681
sn*****@hotmail.com wrote:
I started creating a simple "bits" class, intended to act like a array
of bits. This was my initial idea, basically just overriding the
string representation to display the bitmask (so far):

class bits(long):
def __str__ (self):
s = ''
if self == 0L:
s += '-'
else:
x = self
while x >= 1:
if x & 1L: s = '#' + s
else: s = '-' + s
x >>= 1
return s

My thought was this will act like a Long, and so should act like an
array of bits with arbitrary precision, supporting the standard
bitwise operations, along with others I may add.
>>>import bits
b = bits.bits(32)
b
32L
>>>print b
#-----
>>>b = bits.bits(35)
print b
#---##
>>>b = bits.bits(36)
print b
#--#--
>>>b <<= 1
print b
72

So obviously when I attempt a bitwise operation (ex. b <<= 1), b is
being assigned the result of b << 1, which is a Long.
>>>b = bits.bits(36)
type(b)
<class 'bits.bits'>
>>>type (b << 1)
<type 'long'>

Is there any way around this without reimplementing all of the bitwise
operators? It's not the biggest deal to reimplement the operators, but
it kind of defeats the benefit of inheriting from an integer type.
No, there isn't. How is the existing operation of the operator going to know
that you want an instance of bits instead of long? It can't possibly know
that, even if it looked at the class of one or even both of it's operands:
it could be that instantiating a bits-object needed a reference to
TheCommonApplicationContextThingy or so....
If there isn't a way around this, then I am curious in what situations
it would be beneficial to inherit from an int or long. This issue
seems more related to numeric types, since they are so centered around
operations that involve assignment, as opposed to, say, a List, which
has some operators (ex. +=), but does not depend on them (ex. use
append() instead).
It's beneficial if you want special behavior - but I don't see how the
requirement of overloading the methods to make thinks work affect the grade
of "beneficiality".

Diez
Oct 2 '07 #2

<sn*****@hotmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
|I started creating a simple "bits" class, intended to act like a array
| of bits. This was my initial idea, basically just overriding the
| string representation to display the bitmask (so far):

For this purpose, for the reason you discovered, simply writing a bit_rep
function would likely be better ;-)

[discovery of need to override possibly all of long's special methods]

|| Is there any way around this without reimplementing all of the bitwise
| operators? It's not the biggest deal to reimplement the operators, but
| it kind of defeats the benefit of inheriting from an integer type.

Since the code needed is mostly boilerplate, I think the standard Python
distribution could and should contain builtin type subclass template files,
at least for int/long/float for the reasons you give below.

| If there isn't a way around this, then I am curious in what situations
| it would be beneficial to inherit from an int or long.

As I already said, if you only want to change __str__, consider a
standalone function. If you want to change the behavior of any of the
operations and still tie into the syntax, then the alternative, wrapping
the builtin, may be worse.

class mysub(int):
...
def __add__(self, other): return mysub(self,other)

versus

class mywrap():
def __init__(self,dat):
self._int = dat # should test type
def __add__(self, other): return mywrap(self._int + other._int)

| This issue
| seems more related to numeric types, since they are so centered around
| operations that involve assignment, as opposed to, say, a List, which
| has some operators (ex. +=), but does not depend on them (ex. use
| append() instead).

Terry Jan Reedy

Oct 2 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: shaun roe | last post by:
I want something which is very like a bitset<64> but with a couple of extra functions: set/get the two 32 bit words, and conversion to unsigned long long. I can do this easily by inheriting from...
11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain...
6
by: Russell Mangel | last post by:
Jeffery Richter makes the following statement in two books, the first was written in 2004, the latter in 2002. "You should not define new exception classes derived from ApplicationException; use...
2
by: Charles Law | last post by:
I want a set of controls that all have a border, like a group box. I thought I would create a base control containing just a group box from which my set of controls could inherit. Assuming that...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
7
by: Alex Gusarov | last post by:
Hello, I have strong .NET background with C# and want to do some familiar things from it with Python, but don't know how. For example, I created form in qt designer with QCalendarWidget, translated...
0
by: David Boddie | last post by:
On Mon May 26 17:37:04 CEST 2008, Alex Gusarov wrote: Right. I vaguely remember someone showing something like this at EuroPython a couple of years ago. I believe that this approach is actually...
1
by: Arthur Dent | last post by:
Hello all... I have a method which returns a KeyValuePair(Of Long, String). I would like to make an alias for that, so instead of typing KeyValuePair(Of Long, String) everywhere I could just...
4
by: AalaarDB | last post by:
struct base { int x, y, z; base() {x = 0; y = 0; z = 0;}; base(int x1, int y1, int z1) {x = x1; y = y1; z = z1;}; }; struct intermediate1 : public virtual base {}; struct intermediate2 :...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.