Using Functions

Thread Solved

Join Date: Oct 2009
Posts: 30
Reputation: Garrett85 is an unknown quantity at this point 
Solved Threads: 0
Garrett85 Garrett85 is offline Offline
Light Poster

Using Functions

 
0
  #1
28 Days Ago
How do you know when to use a function by wraping the content between parentheses "function(x) or through dot notation x.function()
? thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven
 
0
  #2
28 Days Ago
Originally Posted by Garrett85 View 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.
You are somewhat confused about functions.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,003
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #3
28 Days Ago
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; 28 Days Ago at 3:13 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 148
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 46
snippsat snippsat is offline Offline
Junior Poster
 
1
  #4
28 Days Ago
Think maybe you are confused about calling a function and calling a method.

  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.
  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. >>>
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 193
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 47
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Junior Poster
 
0
  #5
28 Days Ago
Originally Posted by snippsat View Post
Think maybe you are confused about calling a function and calling a method.

  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.
  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
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 148
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 46
snippsat snippsat is offline Offline
Junior Poster
 
0
  #6
28 Days Ago
like the dir( x ) - really useful
Doc string and help is ok to know about to.
  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. >>>
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 193
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 47
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Junior Poster
 
0
  #7
28 Days Ago
thx a lot --- again really useful
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC