Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-04-2007, 10:28 PM   PM User | #1
chipwhisperer
New to the CF scene

 
Join Date: Jan 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
chipwhisperer is an unknown quantity at this point
Simple Python Syntax Questions

Hey all,

would really appreciate some help with this:

1) I see in code that people will define subclasses like this:

def __init__(self, **config):
server = config.get('server', 'dnd')

what does the ** mean? I've also seen it as one asterisk. Or is that different?

2) i have also seen people define methods of classes with one underscore in front of the name and this is somehow special. why?

I know these may be basic questions, but I've never learned python formally and need to get on these.

Thanks to anyone who can help?
chipwhisperer is offline   Reply With Quote
Old 02-05-2007, 10:19 AM   PM User | #2
Kakao
Regular Coder

 
Join Date: Mar 2006
Location: Brasília, Brazil
Posts: 153
Thanks: 0
Thanked 0 Times in 0 Posts
Kakao is on a distinguished road
Quote:
Originally Posted by chipwhisperer View Post
1) I see in code that people will define subclasses like this:

def __init__(self, **config):
server = config.get('server', 'dnd')

what does the ** mean? I've also seen it as one asterisk. Or is that different?
A parameter starting with ** means the function/method accepts named parameters that will be available as a dictionary:
Code:
>>> def f(**keywords):
...    for key in keywords:
...       print key, keywords[key]
...
>>> f(var1=1, var2=3)
var1 1
var2 3
When the parameter starts with a single * it means positional arguments are accepted:
Code:
>>> def f(*a_tuple):
...    for item in a_tuple:
...       print item
...
>>> f(1,7,'car')
1
7
car
Quote:
Originally Posted by chipwhisperer View Post
2) i have also seen people define methods of classes with one underscore in front of the name and this is somehow special. why?
It is a warning to other programmers that it is a private method and should not be used directly. It is not enforced by the interpreter.
Kakao is offline   Reply With Quote
Old 02-07-2007, 03:07 PM   PM User | #3
chipwhisperer
New to the CF scene

 
Join Date: Jan 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
chipwhisperer is an unknown quantity at this point
Wow! What a great explanation! Thank you so much!
chipwhisperer is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:00 AM.


Advertisement
Log in to turn off these ads.