Re: confused about the different built-in functions in Python

2014-05-26 Thread Deb Wyatt
snip
 
 On 5/25/14 7:55 PM, Deb Wyatt wrote:
 I am confused about how various built-in functions are called.  Some are
 called with dot notation
 
snip
 How do you know/remember which way to call them?
 
 TIA,
 Deb in WA, USA
 
 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.
 
 --
 Ned Batchelder, http://nedbatchelder.com
 
 --
 https://mail.python.org/mailman/listinfo/python-list

Thank you for answering.  I meant to send this to the tutor list, but messed 
up.  So, I guess there isn't a magic answer to this one, and I'll learn as I 
learn the language.  Have a great day.

Deb in WA, USA


FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
Check it out at http://www.inbox.com/earth


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: confused about the different built-in functions in Python

2014-05-26 Thread Terry Reedy

On 5/26/2014 11:15 AM, Deb Wyatt wrote:

snip


On 5/25/14 7:55 PM, Deb Wyatt wrote:

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


snip

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



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.


Part of the answer is Python's history. Up to about 2.1, most built-in 
types did not have methods, though I know lists did. Ints and strings 
did not, or chr and ord might have been int.chr() and str.ord(). (The 
current string methods were originally functions in the string module.)



Thank you for answering.  I meant to send this to the tutor list, but messed up.

 So, I guess there isn't a magic answer to this one, and I'll learn
 as I learn the language.  Have a great day.



--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Re: confused about the different built-in functions in Python

2014-05-26 Thread Marko Rauhamaa
Terry Reedy tjre...@udel.edu:

 Part of the answer is Python's history. Up to about 2.1, most built-in
 types did not have methods, though I know lists did. Ints and strings
 did not, or chr and ord might have been int.chr() and str.ord(). (The
 current string methods were originally functions in the string
 module.)

Ints still aren't quite like regular objects. For example:

x = 500
x.__str__ is x.__str__
   False


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: confused about the different built-in functions in Python

2014-05-26 Thread Christian Heimes
On 26.05.2014 21:00, Marko Rauhamaa wrote:
 Terry Reedy tjre...@udel.edu:
 
 Part of the answer is Python's history. Up to about 2.1, most built-in
 types did not have methods, though I know lists did. Ints and strings
 did not, or chr and ord might have been int.chr() and str.ord(). (The
 current string methods were originally functions in the string
 module.)
 
 Ints still aren't quite like regular objects. For example:
 
 x = 500
 x.__str__ is x.__str__
False

Just like every other object:

 class Example(object): pass
...
 e = Example()
 e.__str__ is e.__str__
False

Python creates a new bound method object every time. A bound method
object is a callable object that keeps a strong reference to the
function, class and object. The bound method object adds the object as
first argument to the function (aka 'self').

Christian


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: confused about the different built-in functions in Python

2014-05-26 Thread Marko Rauhamaa
Christian Heimes christ...@python.org:

 Python creates a new bound method object every time. A bound method
 object is a callable object that keeps a strong reference to the
 function, class and object. The bound method object adds the object as
 first argument to the function (aka 'self').

I stand corrected. I had thought the trampoline (bound method object)
was created once and for all.


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: confused about the different built-in functions in Python

2014-05-26 Thread Marko Rauhamaa
Marko Rauhamaa ma...@pacujo.net:

 Christian Heimes christ...@python.org:

 Python creates a new bound method object every time. A bound method
 object is a callable object that keeps a strong reference to the
 function, class and object. The bound method object adds the object as
 first argument to the function (aka 'self').

 I stand corrected. I had thought the trampoline (bound method
 object) was created once and for all.

Sure enough. The principle is explicitly specified in URL:
https://docs.python.org/3.2/reference/datamodel.html#index-46.

Thus:

class X:
   ...   def f(self):
   ... print(Hello)
   ... 
x = X()
x.f()
   Hello
def f(): print(Meh)
   ... 
x.f = f
x.f()
   Meh
delattr(x, f)
x.f()
   Hello

IOW, you can override a method with setattr() but you cannot delete a
method with delattr().


Marko
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: confused about the different built-in functions in Python

2014-05-26 Thread Gregory Ewing

Marko Rauhamaa wrote:


IOW, you can override a method with setattr() but you cannot delete a
method with delattr().


Actually, you can -- but you need to delete it from
the class, not the instance:

 delattr(X, 'f')
 x.f()
Traceback (most recent call last):
  File stdin, line 1, in module
AttributeError: 'X' object has no attribute 'f'

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list


Re: confused about the different built-in functions in Python

2014-05-26 Thread Steven D'Aprano
On Mon, 26 May 2014 23:58:37 +0300, Marko Rauhamaa wrote:

 Marko Rauhamaa ma...@pacujo.net:
 
 Christian Heimes christ...@python.org:

 Python creates a new bound method object every time. A bound method
 object is a callable object that keeps a strong reference to the
 function, class and object. The bound method object adds the object as
 first argument to the function (aka 'self').

 I stand corrected. I had thought the trampoline (bound method object)
 was created once and for all.
 
 Sure enough. The principle is explicitly specified in URL:
 https://docs.python.org/3.2/reference/datamodel.html#index-46.
 
 Thus:
 
 class X:
...   def f(self):
... print(Hello)
...
 x = X()
 x.f()
Hello
 def f(): print(Meh)
...
 x.f = f
 x.f()
Meh
 delattr(x, f)
 x.f()
Hello
 
 IOW, you can override a method with setattr() but you cannot delete a
 method with delattr().

Of course you can. You just need to know where methods are found. Hint: 
we write this:

class Example:
def method(self): ...


not this:

class Example:
def __init__(self):
def method(self): ...
self.method = method


Methods are attributes of the class, not the instance. Like all class 
attributes, you can retrieve them by doing a lookup on the instance, you 
can shadow them by storing an attribute of the same name on the instance, 
but you cannot rebind or delete them directly on the instance, since they 
aren't on the instance.



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: confused about the different built-in functions in Python

2014-05-26 Thread Terry Reedy

On 5/26/2014 4:32 PM, Marko Rauhamaa wrote:


I stand corrected. I had thought the trampoline (bound method object)
was created once and for all.


Assuming that bound methods are immutable, this is an implementation 
detail, either way. However, it is common for a specific method to be 
called just once on a specific instance. If you have a mixed-case string 
Ss and want the lowercase version, ss = Ss.lower(), you keep ss around 
as long as needed. If the bound method is needed repeatedly, you can 
keep *that* around too.


stack = []
spush = stack.append
spop = stack.pop

for item in it:
  spush(item)
while stack and condition:
  p = process(spop)
...

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


confused about the different built-in functions in Python

2014-05-25 Thread Deb Wyatt




I am confused about how various built-in functions are called. Some are called with dot notationeach_item.isalpha()and some are called like 'normal'sum(numlist)How do you know/remember which way to call them?TIA,Deb in WA, USA

Free Online Photosharing - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!




-- 
https://mail.python.org/mailman/listinfo/python-list


Re: confused about the different built-in functions in Python

2014-05-25 Thread Ned Batchelder

On 5/25/14 7:55 PM, Deb Wyatt wrote:

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?

TIA,
Deb in WA, USA


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.


--
Ned Batchelder, http://nedbatchelder.com

--
https://mail.python.org/mailman/listinfo/python-list


Re: confused about the different built-in functions in Python

2014-05-25 Thread Cameron Simpson

On 25May2014 15:55, Deb Wyatt codemon...@inbox.com wrote:

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?


Documentation.

However, some context:

each_item.isalpha() is not a builtin function as such. It is a method of the 
str class.


Whereas sum _is_ a builtin function, a globally known name which can be 
accessed and used without explicitly importing any module.


There's an explicit list of the builtin 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.

Cheers,
Cameron Simpson c...@zip.com.au

Steinbach's Law: 2 is not equal to 3 -- even for large values of 2.
--
https://mail.python.org/mailman/listinfo/python-list