I'm trying to subclass file, overriding the readline method. The
new method definition begins with
def readline(self, size=None):
line = self.file.readline(size)
# etc., etc.
....where the self.file attribute is a regular file object.
This works fine if I invoke the new method with an integer argument,
but if I invoke it without arguments, I get the error
TypeError: an integer is required
....which I suppose comes from the call to self.file.readline(None).
I know that I could rewrite the method like this:
def readline(self, size=None):
if size == None:
line = self.file.readline()
else:
line = self.file.readline(size)
# etc., etc.
....but this seems to me exceptionally awkward. (Actually, it's worse
than awkward: it fails to complain when the overriding method is
called with the argument None. It would be better to test for the
number of arguments passed to the function, but I can't figure out
how to do this either.)
Is there a better idiom?
TIA!
Kynn
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded. 5 2707
kj wrote:
I'm trying to subclass file, overriding the readline method. The
new method definition begins with
def readline(self, size=None):
line = self.file.readline(size)
# etc., etc.
...where the self.file attribute is a regular file object.
This works fine if I invoke the new method with an integer argument,
but if I invoke it without arguments, I get the error
TypeError: an integer is required
...which I suppose comes from the call to self.file.readline(None).
I know that I could rewrite the method like this:
def readline(self, size=None):
if size == None:
line = self.file.readline()
else:
line = self.file.readline(size)
# etc., etc.
...but this seems to me exceptionally awkward. (Actually, it's worse
than awkward: it fails to complain when the overriding method is
called with the argument None. It would be better to test for the
number of arguments passed to the function, but I can't figure out
how to do this either.)
Is there a better idiom?
Since the manual implies that negative values have no effect, try this:
def readline(self, size=-1):
line = self.file.readline(size)
# etc., etc.
I tested this (just barely), and it seems to work as you wish.
Gary Herron
TIA!
Kynn
On Wed, Jul 30, 2008 at 5:52 PM, kj <so***@987jk.com.invalidwrote:
I know that I could rewrite the method like this:
def readline(self, size=None):
if size == None:
line = self.file.readline()
else:
line = self.file.readline(size)
# etc., etc.
...but this seems to me exceptionally awkward. (Actually, it's worse
than awkward: it fails to complain when the overriding method is
called with the argument None.
IMO, the method should have been possible to call with None in the
first place (like str.split and list.sort), and it's not incorrect for
your method to allow it.
It would be better to test for the
number of arguments passed to the function, but I can't figure out
how to do this either.)
You could of course do:
def readline(self, *args):
line = self.file.readline(*args)
# etc., etc.
But this has its drawbacks.
-Miles
In <ma************************************@python.org Miles <se*********@gmail.comwrites:
>On Wed, Jul 30, 2008 at 5:52 PM, kj <so***@987jk.com.invalidwrote:
>I know that I could rewrite the method like this:
def readline(self, size=None): if size == None: line = self.file.readline() else: line = self.file.readline(size) # etc., etc.
...but this seems to me exceptionally awkward. (Actually, it's worse than awkward: it fails to complain when the overriding method is called with the argument None.
>You could of course do:
def readline(self, *args):
line = self.file.readline(*args)
# etc., etc.
>But this has its drawbacks.
Like what? (I'm pretty new to Python and these drawbacks are not
obvious to me.)
TIA!
kynn
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
En Fri, 01 Aug 2008 16:21:26 -0300, kj <so***@987jk.com.invalidescribi�:
In <ma************************************@python.org Miles
<se*********@gmail.comwrites:
>On Wed, Jul 30, 2008 at 5:52 PM, kj <so***@987jk.com.invalidwrote:
>>I know that I could rewrite the method like this:
def readline(self, size=None): if size == None: line = self.file.readline() else: line = self.file.readline(size) # etc., etc.
...but this seems to me exceptionally awkward. (Actually, it's worse than awkward: it fails to complain when the overriding method is called with the argument None.
>You could of course do:
> def readline(self, *args): line = self.file.readline(*args) # etc., etc.
>But this has its drawbacks.
Like what? (I'm pretty new to Python and these drawbacks are not
obvious to me.)
One thing I can think of: The help system (and the code autocompleter
found in some editors, and other introspection tools) can't tell you the
name/number/default value of the arguments anymore: they're all masked
into a generic *args
--
Gabriel Genellina
Gabriel Genellina wrote:
>>> def readline(self, size=None): if size == None: line = self.file.readline() else: line = self.file.readline(size) # etc., etc.
Not obvious from the docs, but readline(-1) should be the same as
readline().
(In 3.0b2, None also works, but this is not yet documented.) So try:
def readline(self, size=-1):
line = self.file.readline(size) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Russ Perry Jr |
last post by:
I'm using "ID" and "Value" in the generic sense here...
Let's say one page I had a <html:select> with a collection like this:
<html:options collection="items" property="key"...
|
by: Ian |
last post by:
Using Access 97 I have a form with several text boxes, I am trying to get
each text box to display the word "Required" for the appropriate boxes on a
new record. A friend gave me the following line...
|
by: Robin Tucker |
last post by:
Hiya,
I'm using mshtml and the web browser control to embed an OLE object in my
..NET application.
What I want to do is get a reference to the "object" embedded therein:
The HTML looks...
|
by: Supra |
last post by:
value of type "Integer" cannot be convert to system.color
Public Sub APIHighlight2(ByVal BgColour As Integer, ByVal FgColour As
Integer)
SelectionHighlightBackColour(BgColour)
Dim rtb As New...
|
by: Progalex |
last post by:
Hi everybody!
I'm creating a very simple VB .NET application that needs to launch the
command line ml.exe compiler.
At the moment I'm redirecting ML's output to a text file and then my app...
|
by: kevin |
last post by:
I have a form and in the form I have a sub that uses a class I instantiate
using
visual basic code: Public oCP As New Rs232 'instantiate the comm port
I need to share this sub with...
|
by: pinkfloydhomer |
last post by:
How do I check if a string contains (can be converted to) an int? I
want to do one thing if I am parsing and integer, and another if not.
/David
|
by: Vincent |
last post by:
Regarding the dropdetect function, defined as follows:
Sub dropdetect(DropFrm As Form, DropCtrl As Control, Button As Integer,
Shift As Integer, X As Single, Y As Single)
Is it possible to...
|
by: Jim |
last post by:
Could somebody tell me why I need the "elif char == '\n'" in the
following code?
This is required in order the pick up lines with just spaces in them.
Why doesn't
the "else:" statement pick this...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |