I am writing a class that is intended to be subclassed. What is the
proper way to indicate that a sub class must override a method?
Thanks,
Jeremy 22 6497
jeremito schrieb:
I am writing a class that is intended to be subclassed. What is the
proper way to indicate that a sub class must override a method?
Thanks,
Jeremy
What do you mean by 'indicate'? Writing it to the docstring of the
class/method? Writing a comment?
class Foo:
"""
When inheriting from Foo, method foo must be
overridden. Otherwise SPAM.
"""
def foo(self):
print 'bar'
class Bar(Foo):
def __init__(self):
Foo.__init__(se lf)
# Has to be defined to override the base class's method
# when inheriting from class Foo. Otherwise: SPAM
def foo(self):
print 'foo'
I don't know any other way.
Thomas
jeremito wrote:
I am writing a class that is intended to be subclassed. What is the
proper way to indicate that a sub class must override a method?
raise NotImplementedE rror
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
At Thursday 4/1/2007 23:52, jeremito wrote:
>I am writing a class that is intended to be subclassed. What is the proper way to indicate that a sub class must override a method?
If any subclass *must* override a method, raise NotImplementedE rror
in the base class (apart from documenting how your class is supposed
to be used).
--
Gabriel Genellina
Softlab SRL
_______________ _______________ _______________ _____
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya! http://www.yahoo.com.ar/respuestas
Gabriel Genellina schrieb:
At Thursday 4/1/2007 23:52, jeremito wrote:
>I am writing a class that is intended to be subclassed. What is the proper way to indicate that a sub class must override a method?
If any subclass *must* override a method, raise NotImplementedE rror in
the base class (apart from documenting how your class is supposed to be
used).
I learn so much from this list. I didn't even know this error existed.
Thomas
Gabriel Genellina wrote:
At Thursday 4/1/2007 23:52, jeremito wrote:
I am writing a class that is intended to be subclassed. What is the
proper way to indicate that a sub class must override a method?
If any subclass *must* override a method, raise NotImplementedE rror
in the base class (apart from documenting how your class is supposed
to be used).
--
Gabriel Genellina
Softlab SRL
Thanks, that's what I needed. Since I am a complete novice at
Exceptions, I'll have to learn about it.
Jeremy
On 2007-01-05, Thomas Ploch <Th**********@g mx.netwrote:
>>I am writing a class that is intended to be subclassed. What is the proper way to indicate that a sub class must override a method?
If any subclass *must* override a method, raise NotImplemented Error in the base class (apart from documenting how your class is supposed to be used).
I learn so much from this list. I didn't even know this error existed.
And remember: even if it didn't, you could have created your
own:
------------------------------foo.py------------------------------
class NotImplementedE rror(Exception) :
pass
def foo():
print "hi there"
msg = "there's a penguin on the telly!"
raise NotImplementedE rror(msg)
print "how are you?"
foo()
------------------------------------------------------------------
$ python foo.py
hi there
Traceback (most recent call last):
File "foo.py", line 10, in ?
foo()
File "foo.py", line 7, in foo
raise NotImplementedE rror(msg)
__main__.NotImp lementedError: there's a penguin on the telly!
A few carefully thought-out exceptions can often eliminate the
need for a lot of messy code.
--
Grant Edwards grante Yow! I'll show you MY
at telex number if you show
visi.com me YOURS...
Grant Edwards schrieb:
On 2007-01-05, Thomas Ploch <Th**********@g mx.netwrote:
>>>I am writing a class that is intended to be subclassed. What is the proper way to indicate that a sub class must override a method? If any subclass *must* override a method, raise NotImplemente dError in the base class (apart from documenting how your class is supposed to be used).
I learn so much from this list. I didn't even know this error existed.
And remember: even if it didn't, you could have created your
own:
Erm, it wasn't me who asked. I just wanted to say that I didn't know
that there is a NotImplementedE rror. Havn't seen it before.
:-)
Thomas
jeremito wrote:
I am writing a class that is intended to be subclassed. What is the
proper way to indicate that a sub class must override a method?
You can't (easily).
If your subclass doesn't override a method, then you'll get a big fat
AttributeError when someone tries to call it. But this doesn't stop
someone from defining a subclass that fails to override the method.
Only when it's called will the error show up. You can, as others have
noted, define a method that raises NotImplementedE rror. But this still
doesn't stop someone from defining a subclass that fails to override
the method. The error still only occurs when the method is called.
There are some advantages to using NotImplementedE rror:
1. It documents the fact that a method needs to be overridden
2. It lets tools such as pylint know that this is an abstract method
3. It results in a more informative error message
But, in the end, if someone wants to define a class that defiantly
refuses to declare a method, you can't stop them.
Carl Banks
On 2007-01-05, Thomas Ploch <Th**********@g mx.netwrote:
>>I learn so much from this list. I didn't even know this error existed.
And remember: even if it didn't, you could have created your own:
Erm, it wasn't me who asked. I just wanted to say that I didn't know
that there is a NotImplementedE rror.
Sorry, I sort of lost track. You can still invent your own
Exceptions anyway. ;) Just don't do what I do and take the
lazy way out:
raise "method not implemented"
That's considered bad form these days.
--
Grant Edwards gr****@visi.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dalan |
last post by:
I have created a table and form for allowing the input of a unique
customer number (not an ID autonumber number) which a customer would
enter once after installing the database. I'm using DLookup to obtain
the value which works. What I need assistance with is the best method
to use to ensure that only ONE customer number can be entered. A quick
attempt to structure a piece of code is shown below. Any assistance
will be appreciated. Thanks,...
|
by: James Radke |
last post by:
Hello,
I have a web application that contains class 'X' (note that this is one
class of many contained in the application). Now, we need to create a pc
based windows application which will use the same class 'X'. So, I was
wondering, what is the best method for sharing the class between the two
different applications (web based and pc windows based) so that I do not
need to copy the code from one application to the other, and keep...
|
by: Amanda |
last post by:
User is going to be transferring items out of combobox to listbox and
vice versa keeping ascending and desending order respectively at all
time.
If user selects the last item in combobox, the program is supposed to
display a message box stating that there is no more item in the
combobox with OK button to be clicked as the only option and upon
clicking OK, the program should terminate, i.e the program should check
to ensure that the...
|
by: Ed Jensen |
last post by:
I'm really enjoying using the Python interactive interpreter to learn
more about the language. It's fantastic you can get method help right
in there as well. It saves a lot of time.
With that in mind, is there an easy way in the interactive interpreter
to determine which exceptions a method might raise? For example, it
would be handy if there was something I could do in the interactive
interpreter to make it tell me what exceptions...
|
by: ambikasd |
last post by:
Hi,
Can anyone tell me what is dynamic method dispatch? And in what scenario it is usefull?
| |
by: gsspriya07 |
last post by:
what is the method used for string comparison
|
by: Hussein B |
last post by:
Hi,
I'm familiar with static method concept, but what is the class method?
how it does differ from static method? when to use it?
--
class M:
def method(cls, x):
pass
method = classmethod(method)
--
|
by: thisismykindabyte |
last post by:
Hello,
I was wondering if anyone could refer me to a good website that details what exactly makes up the GET and POST methods of a HttpRequest/Response class?
Basically, I am trying to figure out how do you POST data to a website. Many examples seem to be using the URL bar for POSTing, like submitting a search to Google for example is something like "Searchstringhere"&button=click if I'm not mistaken. But if I'm not wrong the POST method...
|
by: nick belshaw |
last post by:
using PyGtk - no problems.
I can draw onto an Image using a PixBuf, Drawable etc
pixbuf = image.get_pixbuf()
drawable = pixbuf.render_pixmap_and_mask()
drawable.draw_line(drawable.new_gc(),10,20,100,120)
pixbuf.get_from_drawable(drawable,cmap,0,0,0,0,100,120
However...
if I read about render_pixmap_and_mask I am told..
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |