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?
Quoting https://www.python.org/dev/peps/pep-0008/#package-and-module-names:
Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.
For classes:
Class names should normally use the CapWords convention.
And function and (local) variable names should be:
lowercase, with words separated by underscores as necessary to improve readability
See this answer for the difference between a module, class and package:
- A Python module is simply a Python source file, which can expose classes, functions and global variables.
- A Python package is simply a directory of Python module(s).
So PEP 8 tells you that:
- modules (filenames) should have short, all-lowercase names, and they can contain underscores;
- packages (directories) should have short, all-lowercase names, preferably without underscores;
- classes should use the CapWords convention.
PEP 8 tells that names should be short; this answer gives a good overview of what to take into account when creating variable names, which also apply to other names (for classes, packages, etc.):
- variable names are not full descriptors;
- put details in comments;
- too specific name might mean too specific code;
- keep short scopes for quick lookup;
- spend time thinking about readability.
To finish, a good overview of the naming conventions is given in the Google Python Style Guide.
Here is a link for different types of Python name conventions:
Type Public Internal Packages lower_with_underModules lower_with_under_lower_with_underClasses CapWords_CapWordsExceptions CapWordsFunctions lower_with_under()_lower_with_under()Global/Class Constants CAPS_WITH_UNDER_CAPS_WITH_UNDERGlobal/Class Variables lower_with_under_lower_with_underInstance Variables lower_with_under_lower_with_underMethod Names lower_with_under()_lower_with_under()Function/Method Parameters lower_with_underLocal Variables lower_with_under
The style guide for Python is based on Guido’s naming convention recommendations.