abc don't play well with private method

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed May 5 11:24:18 EDT 2010


mouadino a écrit :
> i have a problem here :
> 
> i want to make a plugin architecture using abstract base class , this
> is my base class :
> 
> # -*- coding: utf-8 -*-
> 
> import abc
> 
> 
> class BASE_Connector:
>     """
>     Mount point for plugins which refer to actions that can be
> performed.
> 
>     Plugins implementing this reference should provide the following
> methods:
> 
>     connect     the connection method that will initialize a transport
> layer
> 
>     __login     the login  method to use to login in to the server
>                 this one should be called from connect directly

Removing one of the leading underscores would solve your problem.

>     """
>     __metaclass__ = abc.ABCMeta
> 
> 
> 
>     @abc.abstractmethod
>     def __login(self):
>         """ private method to login to the remote server .
>             it use the user name and the password or the private key
>             for that
> 
>         """


 > and when i test my code i get this error :
 > Can't instantiate abstract class FTP_Connector with abstract methods
 > _BASE_Connector__login
 >
 > i can understand why. but my problem is why why ? i can't put
 > "private" method in the ABC class ???

There's no such thing as a "private" attribute in Python. The 
name-mangling mechanism invoked by "__name" is really meant to avoid 
accidental redefinition of the attribute in a derived class.

In this case, your attribute is expected to be redefined, so you 
definitly don't want any name mangling here.

Also and FWIW, the naming convention for "implementation attributes" is 
a single leading underscore.

Also and FWIW:

> 
> class FTP_Connector(FTP, Base_Connector) :
>     """ this class is for the connection to a ftp server
> 
>     """

>     def close(self):
> 
>         self.close()
> 
>     def quit(self):
> 
>         self.quit()
> 

Don't you see kind of a problem here ? Something like, say, an infinite 
recursion ?

HTH



More information about the Python-list mailing list