Re: python: what's with the underscores?

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

I was trying to avoid going here since it may not work and will take some time, but are you at all interested in a shortish session on Skype? I think being able to look at the code and discuss it directly would help a lot. If you do not want to, I understand and that is fine, too, but I think it would be better than emailing back and forth.



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:44
Subject: Re: python: what's with the underscores?


On 12/27/2009 8:33 PM, Alex Hall wrote:
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?

The facebook object is defined in src\buffer\facebook\main.py. It is passed to this class because it can handle what to do with the information it receives in RetrieveNewPosts.

class statuses(facebook):

#why no colon here?  My error. There should be.
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? The facebook api_key and secret_key are passed to facebook in src\session\facebook\main.py in the FinishInitialization function, therefore we don't need them here. The self.session tells the function to look in the current session for the var called facebook which already has all the necessary parameters for this call..
 statuses = self.session.Facebook.statuses.get()
 except:
  logging.exception("Unable to retrieve current statuses.")
  return
 return statuses
Hope this helps, and if not, shout at me until it does.
Haden


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


__________
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: