954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using Functions

How do you know when to use a function by wraping the content between parentheses "function(x) or through dot notation x.function()
? thanks.

Garrett85
Posting Whiz in Training
202 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 
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.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

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

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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']
>>>
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

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 :)

masterofpuppets
Posting Whiz in Training
272 posts since Jul 2009
Reputation Points: 20
Solved Threads: 74
 
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

>>>
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

thx a lot --- again really useful :)

masterofpuppets
Posting Whiz in Training
272 posts since Jul 2009
Reputation Points: 20
Solved Threads: 74
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You