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 2684
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: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |