| | |
Using Functions
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
0
#3 Oct 30th, 2009
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
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.
May 'the Google' be with you!
1
#4 Oct 30th, 2009
Think maybe you are confused about calling a function and calling a method.
So do this.
Python Syntax (Toggle Plain Text)
def my_func(name, greeting = 'Hello'): print '%s, %s' % (greeting, name) #Call a function my_func('tom') #output <Hello tom> #-----------------------------------# class myClass(object): # Inside a class this is called a method def my_method(self,name,greeting = 'Hello'): print '%s, %s' % (greeting, name) a = myClass() #Class instance # Call a method a.my_method('tom') #output <Hello tom>
So do this.
Python Syntax (Toggle Plain Text)
>>> x = 'a string' >>> dir(x) ['__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'] >>> # Now we se method for string >>> # And calling a method x.<name of method> >>> x.upper() 'A STRING' >>> x.split(' ') ['a', 'string'] >>>
0
#5 Oct 30th, 2009
•
•
•
•
Think maybe you are confused about calling a function and calling a method.
Python Syntax (Toggle Plain Text)
def my_func(name, greeting = 'Hello'): print '%s, %s' % (greeting, name) #Call a function my_func('tom') #output <Hello tom> #-----------------------------------# class myClass(object): # Inside a class this is called a method def my_method(self,name,greeting = 'Hello'): print '%s, %s' % (greeting, name) a = myClass() #Class instance # Call a method a.my_method('tom') #output <Hello tom>
So do this.
Python Syntax (Toggle Plain Text)
>>> x = 'a string' >>> dir(x) ['__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'] >>> # Now we se method for string >>> # And calling a method x.<name of method> >>> x.upper() 'A STRING' >>> x.split(' ') ['a', 'string'] >>>
0
#6 Oct 30th, 2009
•
•
•
•
like the dir( x ) - really useful
Python Syntax (Toggle Plain Text)
>>> l = [] >>> type(l) <type 'list'> >>> dir(l) ['__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'] >>> #Now let get help for method in list class >>> l.pop.__doc__ 'L.pop([index]) -> item -- remove and return item at index (default last).\nRaises IndexError if list is empty or index is out of range.' >>> #Better to use print because of new line(\n) >>> print l.pop.__doc__ L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. >>> print l.insert.__doc__ L.insert(index, object) -- insert object before index >>> #With help >>> help(l.insert) Help on built-in function insert: insert(...) L.insert(index, object) -- insert object before index >>> help(l.append) Help on built-in function append: append(...) L.append(object) -- append object to end >>>
![]() |
Other Threads in the Python Forum
- Previous Thread: Need help on hangman game on python
- Next Thread: Class testing
Views: 272 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Python
address anydbm app bash beginner bits calling class code conversion coordinates copy dan08 dictionary directory dynamic edit examples excel feet file float format ftp function generator gui halp homework http i/o images import info input ip itunes java keycontrol line linux list lists loop maintain millimeter mouse newb number numbers output panel parsing path port prime print program programming projects py-mailer py2exe pygame pyqt python queue random rational recursion recursive scrolledtext server smtp split ssh statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode update urllib urllib2 variable whileloop windows write wxpython






