Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #72083 > unrolled thread
| Started by | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| First post | 2014-05-26 14:51 -0400 |
| Last post | 2014-05-26 22:00 -0400 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: confused about the different built-in functions in Python Terry Reedy <tjreedy@udel.edu> - 2014-05-26 14:51 -0400
Re: confused about the different built-in functions in Python Marko Rauhamaa <marko@pacujo.net> - 2014-05-26 22:00 +0300
Re: confused about the different built-in functions in Python Christian Heimes <christian@python.org> - 2014-05-26 21:47 +0200
Re: confused about the different built-in functions in Python Marko Rauhamaa <marko@pacujo.net> - 2014-05-26 23:32 +0300
Re: confused about the different built-in functions in Python Marko Rauhamaa <marko@pacujo.net> - 2014-05-26 23:58 +0300
Re: confused about the different built-in functions in Python Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-05-27 11:14 +1200
Re: confused about the different built-in functions in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-05-27 01:21 +0000
Re: confused about the different built-in functions in Python Terry Reedy <tjreedy@udel.edu> - 2014-05-26 22:00 -0400
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-05-26 14:51 -0400 |
| Subject | Re: confused about the different built-in functions in Python |
| Message-ID | <mailman.10353.1401130279.18130.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2014-05-26 22:00 +0300 |
| Message-ID | <87r43gfjqy.fsf@elektro.pacujo.net> |
| In reply to | #72083 |
Terry Reedy <tjreedy@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
[toc] | [prev] | [next] | [standalone]
| From | Christian Heimes <christian@python.org> |
|---|---|
| Date | 2014-05-26 21:47 +0200 |
| Message-ID | <mailman.10356.1401133664.18130.python-list@python.org> |
| In reply to | #72084 |
On 26.05.2014 21:00, Marko Rauhamaa wrote: > Terry Reedy <tjreedy@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
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2014-05-26 23:32 +0300 |
| Message-ID | <87ioosffh3.fsf@elektro.pacujo.net> |
| In reply to | #72087 |
Christian Heimes <christian@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
[toc] | [prev] | [next] | [standalone]
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Date | 2014-05-26 23:58 +0300 |
| Message-ID | <87egzgfe9u.fsf@elektro.pacujo.net> |
| In reply to | #72088 |
Marko Rauhamaa <marko@pacujo.net>:
> Christian Heimes <christian@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
[toc] | [prev] | [next] | [standalone]
| From | Gregory Ewing <greg.ewing@canterbury.ac.nz> |
|---|---|
| Date | 2014-05-27 11:14 +1200 |
| Message-ID | <bui06sFbst8U1@mid.individual.net> |
| In reply to | #72089 |
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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-05-27 01:21 +0000 |
| Message-ID | <5383e89f$0$29978$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #72089 |
On Mon, 26 May 2014 23:58:37 +0300, Marko Rauhamaa wrote:
> Marko Rauhamaa <marko@pacujo.net>:
>
>> Christian Heimes <christian@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/
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-05-26 22:00 -0400 |
| Message-ID | <mailman.10358.1401156020.18130.python-list@python.org> |
| In reply to | #72088 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web