Re: python: what's with the underscores?

  • From: "Alex Hall" <mehgcap@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sun, 27 Dec 2009 20:33:32 -0500

Okay then, here is something I have taken from Qwitter's statuses.py file (src\buffers\facebook). I have tried to insert my questions into it as comments, and formatting will probably be lost.


#what happened to the 'self' thing, and why pass it a Facebook object?
class statuses(facebook):

#why no colon here?
def RetrieveNewPosts(self)
#just an empty list, fine
 statuses = []
 try:
#doesn't this need some sort of arg, like the UID or the app/user/session keys? Where are all those? What does the session class do here? How did Facebook become an object under sessions?
 statuses = self.session.Facebook.statuses.get()
 except:
  logging.exception("Unable to retrieve current statuses.")
  return
 return statuses

Have a great day,
Alex
New email address: mehgcap@xxxxxxxxx
----- Original Message ----- From: "Haden Pike" <haden.pike@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Sunday, December 27, 2009 20:27
Subject: Re: python: what's with the underscores?


That was a mistake, but I'm glad to see that you sort of understood it if you caught that. It should be:

 def Add(self, x, y):

Haden

On 12/27/2009 6:20 PM, Alex Hall wrote:
In the add function, why did you only declare x and y as params, why not self as well?


Have a great day,
Alex
New email address: mehgcap@xxxxxxxxx
----- Original Message ----- From: "Haden Pike" <haden.pike@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Sunday, December 27, 2009 18:16
Subject: Re: python: what's with the underscores?


When a function is part of a class, you pass self to it as a parameter. Example:

class Myclass():

 def function(self):

To call variables that can be used throughout the class in any functions in that class, I would put self before the variable name. Example:

class Myclass():

 def function(self):
  self.var = "Hello world!"

 def AnotherFunction(self):
  print("The variable value is: %s." % self.var)

To call a function from another function, and there both in the same class, I use self. Example:

class Myclass():

def Add(x, y):
return (x+y)

 def TestAddFunction(self):
  x = 4
  y = 3
  print (self.Add(x, y))

If I want the function to not be a part of the class I would do:

def function():

Now to answer your questions about underscores, Edward already answered part of it. When you create a directory in your python source files, these are called packages. To let python know that it is a package, you must put an __init__.py file in that directory. This can be a blank file or tell python what to automatically import when that package is imported in another file.

HTH
Haden

On 12/27/2009 5:13 PM, Alex Hall wrote:
Okay, but (A) I did not think Python had constructor methods and (B) why do I always see methodName(self): like in your example?


Have a great day,
Alex
New email address: mehgcap@xxxxxxxxx
----- Original Message ----- From: "edward" <personal.edward@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Sunday, December 27, 2009 17:11
Subject: RE: python: what's with the underscores?


Two underscores signify either a constructor method or a built in method I think. Like the str method. If you want to define string formatting with your class its defined by two underscores before and after the method name.
example __str__(self):


-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Alex Hall
Sent: Sunday, December 27, 2009 3:58 PM
To: Blind Programming List
Subject: python: what's with the underscores?

In python, why are some method names surrounded by two underscores? Even some filenames have these two underscores, though I imagine that is more of a convention than anything else. What do the underscores mean, if anything?


Have a great day,
Alex
New email address: mehgcap@xxxxxxxxx

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: