944,093 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 569
  • Python RSS
Oct 30th, 2009
0

Using Functions

Expand Post »
How do you know when to use a function by wraping the content between parentheses "function(x) or through dot notation x.function()
? thanks.
Reputation Points: 10
Solved Threads: 1
Junior Poster
Garrett85 is offline Offline
190 posts
since Oct 2009
Oct 30th, 2009
0
Re: Using Functions
Click to Expand / Collapse  Quote originally posted by Garrett85 ...
How do you know when to use a function by wraping the content between parentheses "function(x) or through dot notation x.function()
? thanks.
You are somewhat confused about functions.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Oct 30th, 2009
0
Re: Using Functions
In function(x) x is the outside argument passed to the function.
In x.function() x is the namespace of the module or instance used.
You better read up on functions!

See ...
http://www.swaroopch.com/notes/Python_en:Functions
Last edited by vegaseat; Oct 30th, 2009 at 3:13 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 30th, 2009
1
Re: Using Functions
Think maybe you are confused about calling a function and calling a method.

Python Syntax (Toggle Plain Text)
  1. def my_func(name, greeting = 'Hello'):
  2. print '%s, %s' % (greeting, name)
  3.  
  4. #Call a function
  5. my_func('tom') #output <Hello tom>
  6.  
  7. #-----------------------------------#
  8. class myClass(object):
  9. # Inside a class this is called a method
  10. def my_method(self,name,greeting = 'Hello'):
  11. print '%s, %s' % (greeting, name)
  12.  
  13. a = myClass() #Class instance
  14.  
  15. # Call a method
  16. a.my_method('tom') #output <Hello tom>

So do this.
Python Syntax (Toggle Plain Text)
  1. >>> x = 'a string'
  2. >>> dir(x)
  3. ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
  4. >>> # Now we se method for string
  5. >>> # And calling a method x.<name of method>
  6. >>> x.upper()
  7. 'A STRING'
  8. >>> x.split(' ')
  9. ['a', 'string']
  10. >>>
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is online now Online
771 posts
since Aug 2008
Oct 30th, 2009
0
Re: Using Functions
Click to Expand / Collapse  Quote originally posted by snippsat ...
Think maybe you are confused about calling a function and calling a method.

Python Syntax (Toggle Plain Text)
  1. def my_func(name, greeting = 'Hello'):
  2. print '%s, %s' % (greeting, name)
  3.  
  4. #Call a function
  5. my_func('tom') #output <Hello tom>
  6.  
  7. #-----------------------------------#
  8. class myClass(object):
  9. # Inside a class this is called a method
  10. def my_method(self,name,greeting = 'Hello'):
  11. print '%s, %s' % (greeting, name)
  12.  
  13. a = myClass() #Class instance
  14.  
  15. # Call a method
  16. a.my_method('tom') #output <Hello tom>

So do this.
Python Syntax (Toggle Plain Text)
  1. >>> x = 'a string'
  2. >>> dir(x)
  3. ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
  4. >>> # Now we se method for string
  5. >>> # And calling a method x.<name of method>
  6. >>> x.upper()
  7. 'A STRING'
  8. >>> x.split(' ')
  9. ['a', 'string']
  10. >>>
hey thank's bro for that information. I didn't know some of that stuff, like the dir( x ) - really useful
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Oct 30th, 2009
0
Re: Using Functions
Quote ...
like the dir( x ) - really useful
Doc string and help is ok to know about to.
Python Syntax (Toggle Plain Text)
  1. >>> l = []
  2. >>> type(l)
  3. <type 'list'>
  4. >>> dir(l)
  5. ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
  6. >>> #Now let get help for method in list class
  7. >>> l.pop.__doc__
  8. 'L.pop([index]) -> item -- remove and return item at index (default last).\nRaises IndexError if list is empty or index is out of range.'
  9.  
  10. >>> #Better to use print because of new line(\n)
  11. >>> print l.pop.__doc__
  12. L.pop([index]) -> item -- remove and return item at index (default last).
  13. Raises IndexError if list is empty or index is out of range.
  14.  
  15. >>> print l.insert.__doc__
  16. L.insert(index, object) -- insert object before index
  17. >>> #With help
  18. >>> help(l.insert)
  19. Help on built-in function insert:
  20.  
  21. insert(...)
  22. L.insert(index, object) -- insert object before index
  23.  
  24. >>> help(l.append)
  25. Help on built-in function append:
  26.  
  27. append(...)
  28. L.append(object) -- append object to end
  29.  
  30. >>>
Reputation Points: 280
Solved Threads: 278
Master Poster
snippsat is online now Online
771 posts
since Aug 2008
Oct 30th, 2009
0
Re: Using Functions
thx a lot --- again really useful
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Need help on hangman game on python
Next Thread in Python Forum Timeline: Class testing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC