I think naming an interface with an I prefix is perfectly acceptable.
e.g.:
IFooIPublishable
A few years back I used Zope Interfaces. I noticed most codebases used this convention. Our team did too.
We preferred IFoo vs. FooInterface or IFooInterface
I think naming an interface with an I prefix is perfectly acceptable.
e.g.:
IFooIPublishable
A few years back I used Zope Interfaces. I noticed most codebases used this convention. Our team did too.
We preferred IFoo vs. FooInterface or IFooInterface
I'm not aware of any community-wide standards in that regard apart from PEP8, which doesn't address this specifically.
I'd suggest to do whatever your team is most comfortable with, but above all else be consistent.
If you're using Python 2.6 or higher, you can use the Abstract Base Class module from the standard library if you want to enforce abstractness. Here's an example:
from abc import ABCMeta, abstractmethod
class SomeAbstractClass(object):
__metaclass__ = ABCMeta
@abstractmethod
def this_method_must_be_overridden(self):
return "But it can have an implementation (callable via super)."
class ConcreteSubclass(SomeAbstractClass):
def this_method_must_be_overridden(self):
s = super(ConcreteSubclass, self).this_method_must_be_overridden()
return s.replace("can", "does").replace(" (callable via super)", "")
Output:
>>> a = SomeAbstractClass()
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
a = SomeAbstractClass()
TypeError: Can't instantiate abstract class SomeAbstractClass with abstract
methods this_method_must_be_overridden
>>> c = ConcreteSubclass()
>>> c.this_method_must_be_overridden()
'But it does have an implementation.'
Based on your last sentence, I would answer answer "just document it". Anyone who uses a class in a way that the documentation says not to must accept responsibility for any strange behavior.
There is an abstract base class mechanism in Python, but I don't see any reason to use it if your only goal is to discourage instantiation.
object oriented design - Does using the word "base" in a class name indicate abstraction? - Software Engineering Stack Exchange
Naming convention for abstract classes
How do you name Interface(Abstract Base classes, etc) modules, classes in python?
oop - Python: how to make abstract class attribute, with all caps naming convention and linter warnings - Stack Overflow
Videos
"I'm wondering if there is a widely accepted convention for naming base classed in OOP"
Short answer: no, there is not.
If you want to read something directly out of the name of a class, you need to consult the programming guidelines of your team or organization. There are only very few widely accepted naming conventions, and even those don't apply to "OOP in general", but usually to a specific language ecosystem.
For example, in C#, I would usually expect a type name starting with a single I to be an interface (though not every team names gives interfaces always an I prefix; it is a convention suggested by Microsoft). For a class ending with the word Exception I would expect it to be a derivation of System.Exception. And (as mentioned in a comment by @Blake, thanks), attribute classes in C# usually end with the suffix Attribute. In Python, the PEP 8 style guide suggests to let exception classes (which represent errors) end with the name Error.
Specifically for C#, I guess that list is complete. I cannot remember to have seen a naming style "broadly accepted" across teams and organizations, where part of a class name induces a clear semantics.
Honestly this reeks of Hungarian Notation. Or to be more correct about it badly applied Hungarian Notation.
Hungarian Notation was meant to express type information that the programming language could not. For example in Assembly it is helpful to know if you are dealing with a pointer to a string, or an int. The language isn't going to track this for you so a local naming standard encoding the type information is particularly helpful.
However this falls flat on its face in languages which have expressive type systems that can encode this information outside of the name. This could by by designating the type as abstract for example or only providing protected constructors only available to deriving types.
A good example of poorly applied Hungarian Notation is the I in a C# interface name like IComparable. Comparable is a sufficiently good name, and the fact that its an interface is surfaced easily enough by looking at its definition, or through an ide.
So i'd ask. Does this language allow me to express in the type system that this class is available for derivation? If so then drop the Base and just call it Car. It easy enough to identify that Model-Y derives from Car.
If the language does not support this ability, then I'd consult in order: You organisational naming conventions, the platform/language naming standards/conventions, the strict need to express this information at all in the name.
Hi.
I didn't find any information about this in PEP8, nor did my Google search reveal any informal rules, so I'm asking here: Say I have this:
import abc
class Repo(abc.ABC):
pass
class RepoImpl(Repo):
passShould I rename rename "RepoImpl" to "Repo" and "Repo" to a) IRepo or b) RepoInterface? Or are there any other conventions the Python community has settled on?
Hey python developers! So, I've hardly written any interfaces or ABC's in python. But, I'm about to build a package which is supposed to act as a base for several teams. I've started writing interfaces, but.. looks like there's no official naming conventions for them in python. So, how do you name them or what do you think is a sensible naming convention?
So I'm learning C++ after spending a good bulk of my time programming in C# and Java.
Because of this, learning C++ has been pretty easy. Mostly just syntax and quirks here and there, but one thing that is difficult to adopt is the fact that C++ does not have interfaces.
That's all well and good, just make a pure abstract base class. My struggle is here however:
Is it wrong to denote one of these classes with an 'I' prefix, such as IPrintable? I have not come across anything like that in C++ code bases and am really wondering how I'm supposed to distinguish my "interfaces" from other concrete classes.
Is there any standard for this? Do people just remember which classes are and aren't/wait for a lint error to throw for not implementing said methods?
According to Bloch's Effective Java (Item 18) the Abstract prefix is a convention used in a special case.
You can combine the virtues of interfaces and abstract classes by providing an abstract skeletal implementation class to go with each nontrivial interface that you export. ... By convention, skeletal implementations are called AbstractInterface, where Interface is the name of the interface they implement.
But Bloch also points out that the name SkeletalInterface would have made sense, but concludes that
the Abstract convention is now firmly established.
As other answers have pointed out, in general there is no reason to apply this naming convention to all abstract classes.
There is no convention. It's all about what will help you, as the developer, code faster and better, and help others understand your code.
Ask the people who will be seeing and maintaining the code. What would they rather see? What will make it easier on them? Then name it based on what they'd like.
On another note, Code Conventions for the Java Programming Language: 9. Naming Conventions suggests no requirement:
Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).