How do you know when to use a function by wraping the content between parentheses "function(x) or through dot notation x.function()
? thanks.
sneekula 969 Nearly a Posting Maven
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.
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
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!
Edited by vegaseat because: n/a
snippsat 661 Master Poster
Think maybe you are confused about calling a function and calling a method.
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.
>>> 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']
>>>
masterofpuppets
Think maybe you are confused about calling a function and calling a method.
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.
>>> 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'] >>>
hey thank's bro for that information. I didn't know some of that stuff, like the dir( x ) - really useful :)
snippsat 661 Master Poster
like the dir( x ) - really useful
Doc string and help is ok to know about to.
>>> 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
>>>
masterofpuppets
thx a lot --- again really useful :)
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.