top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Different built-in functions in Python

+1 vote
34 views

I am confused about how various built-in functions are called. Some are called with dot notation

each_item.isalpha()

and some are called like 'normal'

sum(numlist)

How do you know/remember which way to call them?

posted May 26, 2014 by Garima Jain

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

2 Answers

+1 vote

Some context:

each_item.isalpha() is not a built-in function as such. It is a method of the "str" class.

Whereas "sum" is a built-in function, a globally known name which can be accessed and used without explicitly importing any module.

There's an explicit list of the built-in functions in the Python doco.

For a class, you can look at the doco for the class ("String methods" in the python doco, for the "str" class), or run:

help(str)

at the interactive Python prompt.

answer May 26, 2014 by Jagan Mishra
0 votes

It can be confusing. Generally, built-in functions (like sum, len, etc) are used when the operation could apply to many different types. For example, sum() can be used with any iterable that produces addable things.

Operations that are defined only for a single type (like .isalpha as a string operation) are usually defined as methods on the type.

This is not a black/white distinction, I'm sure there are interesting counter-examples. But this is the general principle.

answer May 26, 2014 by Sheetal Chauhan
Similar Questions
+3 votes

Is the byteorder (or endianness) of the functions in the audioop module somewhere specified or does anyone know how it behaves on different systems?

On my little-endian system it matches the system's endianness:

>>> import sys, audioop
>>> sys.byteorder
'little'
>>> audioop.lin2lin(b'xff', 1, 2)
b'x00xff'
0 votes

I've been using the settrace function to write a tracer for my program, which is working great except that it doesn't seem to work for built-in functions, like open('filename.txt'). This doesn't seem to be documented, so I'm not sure if I'm doing something wrong or that's the expected behavior.

If settrace's behavior in this regard is fixed, is there any way to trace calls to open()? I don't want to use Linux's strace, as it'll run for whole program (not just the part I want) and won't show my python line numbers/file names, etc. The other option I considered was monkey-patching the open function through a wrapper, like:

def wrapped_open(*arg,**kw):
 print 'open called'
 traceback.print_stack()
 f = __builtin__.open(*arg,**kw)
 return f
open = wrapped_open

but that seemed very brittle to me. Could someone suggest a better way of doing this?

0 votes

Both the functions gives same output

str(18)
'18'

repr(18)
'18'

What differences these two functions have in term of functionality and in whic scenarios these two functions are used ?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...