| | |
Using Functions
Thread Solved |
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
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!
•
•
Join Date: Aug 2008
Posts: 148
Reputation:
Solved Threads: 46
1
#4 28 Days Ago
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 28 Days Ago
•
•
•
•
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'] >>>
•
•
Join Date: Aug 2008
Posts: 148
Reputation:
Solved Threads: 46
0
#6 28 Days Ago
•
•
•
•
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
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






