6

I have been working on a project that is using Google App Engine's python sdk. I was digging around in it and saw a lot of methods like this.

def _put(self, bla, bla):
    do something
put = _put

Is there any reason they assign the private variable like this? I mean I don't see the point of defining a method as private and then assigning it to a non private variable. Why not just make the method its self public?

def put(self, bla, bla):
    do something

The only then I can think of is it is done this way to meet some "proper coding style" rules. I mainly interested because I would like to get better at writing proper python code and wanted to know if this is something I should be doing myself?

EDIT: Thanks @falsetru for the link! Here is what I'm talking about (https://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/ext/ndb/model.py?r=410#3340)

  • Looks pretty useless to me. – FogleBird Feb 04 '14 at 14:52
  • 1
    Python doesn't really have private and public methods. The underscore thing is just a convention. – ChrisGPT was on strike Feb 04 '14 at 14:56
  • possible duplicate of [Does python have 'private' variables in classes?](http://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes) – But I'm Not A Wrapper Class Feb 04 '14 at 14:58
  • This way, it's easier to monkey patch class methods if the new method needs to call the original method. – sloth Feb 04 '14 at 15:00
  • I guess whoever wrote it like that just has a funny way of doing things? Maybe they do it to help keep straight what is being called from outside the object and what is being called from inside, by only calling non private (yes I know it not really private) methods from outside and only calling private from inside the object. – amiller2571 Feb 04 '14 at 15:01
  • @Chris this wasn't a question about python having private variables. – amiller2571 Feb 04 '14 at 15:08
  • @amiller2571, What does `__author__ = ...` line read? (https://code.google.com/p/googleappengine/source/browse/trunk/python/google/appengine/ext/ndb/model.py?r=410#3340) – falsetru Feb 04 '14 at 15:09
  • @falsetru `__author__ = 'guido@google.com (Guido van Rossum)` – amiller2571 Feb 04 '14 at 15:11

2 Answers2

3

Guido's explanation for this pattern in the NDB code:

NDB has a different rule for public vs. protected names. Many (but not all) of the names starting with an underscore are actually considered public APIs, because we don't want to reserve their method names as property names. We just alias the more user-friendly underscore-less names for convenience (e.g. the real method is _put(), and you should really use this, but we let you use put() as long as you don't have a property named 'put').

Greg
  • 10,350
  • 1
  • 26
  • 35
2

Only one (a little) reasonable purpose of doing it is closure:

def wrapper():
    x = 2
    def _put(self, bla, bla):
       return x + 2

    self.put = _put
IProblemFactory
  • 9,551
  • 8
  • 50
  • 66