[Python-projects] Type inference not working?
Sylvain Thénault
sylvain.thenault at logilab.fr
Thu Jan 24 08:41:31 CET 2008
On Wed, Jan 23, 2008 at 08:01:33PM +0100, Maarten ter Huurne wrote:
> This is the earlier version, it executes correctly and it triggers the same
> warning:
>
> ===
> class SomeClass(object):
>
> def __m(self):
> pass
>
> @staticmethod
> def s():
> SomeClass().__m()
>
> SomeClass.s()
> ===
>
> The way I want to use it is by having a factory method like this:
> ===
> @staticmethod
> def create(data):
> instance = SomeClass()
> instance.__setSomething(data)
> return instance
> ===
>
> I want to have several ways of instantiating the same class and doing it all
> using different constructor arguments would be messy.
Why not simple instance = SomeClass(data) with SomeClass'__init__
calling __setSomething(data) ?
> syt at logilab.fr wrote on 2008-01-23 07:09:22 PM:
>
> > huum, I'm not sure about what you're trying to acheive and what
> > behaviour you would expect. Here is an exemple of what's ok and what's
> > not (using a private static method since this is the worst case, but it
> > could be a class/instance method as well) ::
> >
> > class SomeClass(object):
> >
> > @staticmethod
> > def __m():
> > pass
> >
> > def method(self):
> > # calling private method within the class is ok
> > self.__m()
> >
> > @classmethod
> > def clsmethod(cls):
> > # calling private method within the class is ok
> > cls.__m()
> >
> > @staticmethod
> > def statmethod():
> > # calling private method within the class is ok
> > SomeClass.__m()
> >
> > class AnotherClass(object):
> > @staticmethod
> > def statmethod():
> > # calling private method from another class is not ok
> > SomeClass.__m()
> >
> > # calling private method on an instance is not ok, and that's right
> > SomeClass().__m()
> >
> >
> > Executing pylint on this produce ::
> >
> > W0212: 26:AnotherClass.statmethod: Access to a protected member __m of a
> > client class
> > W0212: 29: Access to a protected member __m of a client class
> >
> > which is what was expected (for me at least ;)
>
> What I'd like to do is call a private instance method from a static method in
> the same class. This is flagged as a warning, which could mean:
> - PyLint considers this as something that is not OK (why?)
> - PyLint doesn't know that the instance is of the same class that the static
> method belongs to (my guess)
no, this time, pylint is right ;) You're actually calling the private
method on an instance outside the class code definition (eg to simplify,
you're only allowed to call private method using "self" inside a method
of the class). You're 2nd point doesn't make sense: when you're calling
a method (static or not) on an instance, the instance should always be
of the same class (or derived of) as the method's class...
--
Sylvain Thénault LOGILAB, Paris (France)
Formations Python, Zope, Plone, Debian: http://www.logilab.fr/formations
Développement logiciel sur mesure: http://www.logilab.fr/services
Python et calcul scientifique: http://www.logilab.fr/science
More information about the Python-Projects
mailing list