473,807 Members | 2,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is not objects in Python?

I have heard some criticism about Python, that it is not fully object-
oriented.

What is not an object in Python?

Why isn't len implemented as a str.len and list.len method instead of
a len(list) function?
Sep 28 '08
41 1617
On Sep 29, 12:08 am, Terry Reedy <tjre...@udel.e duwrote:
George Sakkis wrote:
On Sep 28, 2:29 pm, process <circularf...@g mail.comwrote:
I have heard some criticism about Python, that it is not fully object-
oriented.
That's not a bug, it's a feature ;-)
Why isn't len implemented as a str.len and list.len method instead of
a len(list) function?
As Terry Reedy wrote, partly history and partly practicality. There's
no philosophical reason why we write "len(x)" (generic builtin),
"x.append(1 )" (method) or "del x[i]" (statement). The latter in
particular is IMHO a design wart; there's no reason for not writing it
as "x.delete(i )".

As a general rule and matter of practice, methods that apply to all or
most classes (or all number classes) have built-in functions that call
the corresponding special method (or C-level slot).
It would be easier to justify this rule if it was more clear-cut, and
specifically if it was applied only to methods that are available to
*all* classes (such as type() and getattr()) rather than the ill-
defined "most classes".

George
Sep 29 '08 #11
On Sep 29, 1:44*am, George Sakkis <george.sak...@ gmail.comwrote:
On Sep 29, 12:08 am, Terry Reedy <tjre...@udel.e duwrote:
George Sakkis wrote:
On Sep 28, 2:29 pm, process <circularf...@g mail.comwrote:
>I have heard some criticism about Python, that it is not fully object-
>oriented.
That's not a bug, it's a feature ;-)
>Why isn't len implemented as a str.len and list.len method instead of
>a len(list) function?
As Terry Reedy wrote, partly history and partly practicality. There's
no philosophical reason why we write "len(x)" (generic builtin),
"x.append(1 )" (method) or "del x[i]" (statement). The latter in
particular is IMHO a design wart; there's no reason for not writing it
as "x.delete(i )".
As a general rule and matter of practice, methods that apply to all or
most classes (or all number classes) have built-in functions that call
the corresponding special method (or C-level slot).

It would be easier to justify this rule if it was more clear-cut, and
specifically if it was applied only to methods that are available to
*all* classes (such as type() and getattr()) rather than the ill-
defined "most classes".
That wasn't your original claim, though. You claimed there was no
philosophical reason, then Terry gave you one, then you said, well
there's no clear cut reason. Unless you define "philosophi cal" as
"clear cut" (a definition I'm not sure many would agree with).

Anyway, you are right to claim there's no clear cut distinction, just
as there's never any clear cut distinction over whether something
should be an operator or not. Addition is only available to the ill-
defined "most classes", yet not only is it not a method, it has its
own syntax. There's no clear cut distinction there, it's just a
design decision. Likewise, making len() into a function is just a
design decision, that len is a common enough operation that it need
elevated status. It's really nothing more. Python wouldn't suffer
much regardless if len is a method, a built-in function, or an
operator with its own syntax.
Carl Banks
Sep 29 '08 #12
On Sep 29, 2:34*am, Carl Banks <pavlovevide... @gmail.comwrote :
On Sep 29, 1:44*am, George Sakkis <george.sak...@ gmail.comwrote:
On Sep 29, 12:08 am, Terry Reedy <tjre...@udel.e duwrote:
George Sakkis wrote:
On Sep 28, 2:29 pm, process <circularf...@g mail.comwrote:
I have heard some criticism about Python, that it is not fully object-
oriented.
That's not a bug, it's a feature ;-)
Why isn't len implemented as a str.len and list.len method insteadof
a len(list) function?
As Terry Reedy wrote, partly history and partly practicality. There's
no philosophical reason why we write "len(x)" (generic builtin),
"x.append(1 )" (method) or "del x[i]" (statement). The latter in
particular is IMHO a design wart; there's no reason for not writingit
as "x.delete(i )".
As a general rule and matter of practice, methods that apply to all or
most classes (or all number classes) have built-in functions that call
the corresponding special method (or C-level slot).
It would be easier to justify this rule if it was more clear-cut, and
specifically if it was applied only to methods that are available to
*all* classes (such as type() and getattr()) rather than the ill-
defined "most classes".

That wasn't your original claim, though. *You claimed there was no
philosophical reason, then Terry gave you one, then you said, well
there's no clear cut reason. *Unless you define "philosophi cal" as
"clear cut" (a definition I'm not sure many would agree with).
I won't argue about that, just s/philosophical/clear cut/ then.
Anyway, you are right to claim there's no clear cut distinction, just
as there's never any clear cut distinction over whether something
should be an operator or not. Addition is only available to the ill-
defined "most classes", yet not only is it not a method, it has its
own syntax. *There's no clear cut distinction there, it's just a
design decision.

Likewise, making len() into a function is just a
design decision, that len is a common enough operation that it need
elevated status. It's really nothing more. Python wouldn't suffer
much regardless if len is a method, a built-in function, or an
operator with its own syntax.
I'm not quite sure it's exactly the same. The distinction between
operators and non-operators is syntactically clear cut: operators
consist of punctuation characters and (binary operators) use infix
syntax. Yes, from a pure semantics standpoint this distinction is
redundant; a language could support a method-only syntax, but there is
much value in using terse intuitive symbols for certain domains (with
math being the prominent one). For example I would be much less
opposed to len() being defined as, say, |x| if "|...|" was a valid
operator.

I don't see the same value in creating a distinction between methods
and builtin functions unless the latter are truly generic (and even
then I wouldn't mind having them as methods of the base object class,
e.g. object.type()). Having a builtin len(x) delegate to x.__len__()
seems harder to justify.

George
Sep 29 '08 #13
George Sakkis:
I don't see the same value in creating a distinction between methods
and builtin functions unless the latter are truly generic (and even
then I wouldn't mind having them as methods of the base object class,
e.g. object.type()). Having a builtin len(x) delegate to x.__len__()
seems harder to justify.
I have shown few usage examples of the len() one of the posts in this
thread. Can you take a look at them and show how you can better
rewrite them without a len function?

Bye,
bearophile
Sep 29 '08 #14
On Sep 29, 9:02 am, bearophileH...@ lycos.com wrote:
George Sakkis:
I don't see the same value in creating a distinction between methods
and builtin functions unless the latter are truly generic (and even
then I wouldn't mind having them as methods of the base object class,
e.g. object.type()). Having a builtin len(x) delegate to x.__len__()
seems harder to justify.

I have shown few usage examples of the len() one of the posts in this
thread. Can you take a look at them and show how you can better
rewrite them without a len function?

Bye,
bearophile
You mean this ?
>>seq = ["aaaa", "bb", "c", "ddd"]
seq2 = [[1,1,1,1], [2,2], [3], [4,4,4]]
sorted(seq, key=lambda x:x.__len__())
['c', 'bb', 'ddd', 'aaaa']
>>sorted(seq2 , key=lambda x:x.__len__())
[[3], [2, 2], [4, 4, 4], [1, 1, 1, 1]]

Sure, "len" looks better than lambda x:x.__len__(), but the same would
be true if there was an "upper" builtin for the following example:
>>s = ['a', 'd', 'B', 'C']
s2 = [u'a', u'd', u'B', u'C']
upper = lambda x: x.upper()
sorted(s, key=upper)
['a', 'B', 'C', 'd']
>>sorted(s2, key=upper)
[u'a', u'B', u'C', u'd']

No difference in principle, just len() happens to be implemented more
often than upper().

George
Sep 29 '08 #15
Mel
George Sakkis wrote:
As Terry Reedy wrote, partly history and partly practicality. There's
no philosophical reason why we write "len(x)" (generic builtin),
"x.append(1 )" (method) or "del x[i]" (statement). The latter in
particular is IMHO a design wart; there's no reason for not writing it
as "x.delete(i )".
`del x` has almost nothing to do with `x`, and almost everything to do with
a namespace containing `x`. The object doesn't know what namespace it's
in.

Mel.

Sep 29 '08 #16
On Mon, 29 Sep 2008 11:14:36 -0400, Mel wrote:
George Sakkis wrote:
>As Terry Reedy wrote, partly history and partly practicality. There's
no philosophical reason why we write "len(x)" (generic builtin),
"x.append(1) " (method) or "del x[i]" (statement). The latter in
particular is IMHO a design wart; there's no reason for not writing it
as "x.delete(i )".

`del x` has almost nothing to do with `x`, and almost everything to do
with a namespace containing `x`. The object doesn't know what namespace
it's in.
But George's example was ``del x[i]`` which *has* something to do with
`x` as it mutates the object (or at least calls `x.__delitem__( )`).

Ciao,
Marc 'BlackJack' Rintsch
Sep 29 '08 #17
George Sakkis:
No difference in principle, just len() happens to be implemented more
often than upper().
That's an important point. In a language that tries to be both
practical, readable, and elegant, the things that are done more may
deserve some sugar, to avoid code like this in many cases:

sorted(seq, key=lambda x:x.__len__())

Bye,
bearophile
Sep 29 '08 #18
On Sep 29, 11:37 am, bearophileH...@ lycos.com wrote:
George Sakkis:
No difference in principle, just len() happens to be implemented more
often than upper().

That's an important point. In a language that tries to be both
practical, readable, and elegant, the things that are done more may
deserve some sugar, to avoid code like this in many cases:

sorted(seq, key=lambda x:x.__len__())
If this was the most compelling use case of having len as builtin, I
would be even less convinced. How often do you sort by len? FWIW, the
most common sorting keys I see in real world are attrgetter(some _attr)
or itemgetter(some _index) and both are imported functions, not
builtins.

George
Sep 29 '08 #19
George Sakkis wrote:
Sure, "len" looks better than lambda x:x.__len__(), but the same would
be true if there was an "upper" builtin for the following example:
>>>s = ['a', 'd', 'B', 'C']
s2 = [u'a', u'd', u'B', u'C']
upper = lambda x: x.upper()
sorted(s, key=upper)
['a', 'B', 'C', 'd']
>>>sorted(s2, key=upper)
[u'a', u'B', u'C', u'd']

No difference in principle, just len() happens to be implemented more
often than upper().
I disagree. One versus many classes is a principle. If a method is
used on objects of just one class, the class attribute can be used and
passed directly.

sorted(s, key = str.upper)
sorted(s2, key = unicode.upper)

The problem is having two text classes, which Guido considers to be a
mess and which Python did not have originally and which 3.0 also does
not have, even though bytes keeps the string methods. Mixing int, long,
and float numbers is or should be more common than mixing str and
unicode strings, and certainly more common than mixing str strings and
bytes in 3.0 (which is actively discouraged).

To me, discerning the implicit principles of Python's design makes it
easier to learn and remember, even if they are not as exact as one might
like. The choices are not random, and so one can do better than rote
memory.

Terry Jan Reedy

Sep 29 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3653
by: Ng Pheng Siong | last post by:
Hi, I just noticed that demo/evp_ciph_test.py of my M2Crypto package causes a core dump. It used to not do that. ;-) The core dump happens when the program is exiting; in the output below, rc2_40_cbc is the last test run by the program: $ python evp_ciph_test.py ...
220
19201
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
54
6588
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO FRICKIN' COOL!!! ***MAN*** that would save me a buttload of work and make my life sooooo much easier!" As opposed to minor differences of this feature here, that feature there. Variations on style are of no interest to me. I'm coming at this from a...
28
3312
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are a number of aspects to this simplification, but for me the unification of methods and functions is the biggest benefit. All methods look like functions (which students already understand). Prototypes (classes) look like modules. This will...
7
2402
by: Bo Peng | last post by:
Dear Python group: I am planning on an application that involves several complicated C++ classes. Basically, there will be one or two big data objects and some "action" objects that can act on the data. I would like to use a script language to control the interaction between these c++ objects. I become interested in Python since it can load C++ objects and can even extend C++ classes. However, I am not quite sure to what extent can...
2
1415
by: Sells, Fred | last post by:
I have a legacy system with data stored in binary files on a remote server. I need to access and modify the content of those files from a webserver running on a different host. (All Linux) I would like to install a server on the legacy host that would use my python code to translate between the legacy files and Python Objects that represent the subset of data I care about, then pass those Python objects back and forth to my webserver,...
137
7209
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very pragmatic - 3) I usually move forward when I get the gut feeling I am correct - 4) Most likely because of 1), I usually do not manage to fully explain 3) when it comes true. - 5) I have developed for many years (>18) in many different environments,...
21
1620
by: godwin | last post by:
Hi all, I wanna thank Martin for helping out with my ignorance concerning execution of stored procedure with python. Now i have decided to write a web app that googles into my companies proprietary database. I need to know whether zope is good for that job. But even the introduction to zope in the zope book was intimidating. Are there any simple options? If so plz tell me how i can obtain it and study it. I should say that i am new to web...
4
1323
by: Gre7g Luterman | last post by:
I suppose I was lulled into complacency by how Python makes so many things look like classes, but I'm starting to realize that they're not, are they? I'm writing a C program which handles Python objects in different ways based on their type. I do a PyInstance_Check(PyObj) to determine if the PyObj is an instance, but it is returning 0 on a lot of stuff that I thought would be an instance. So I did the following simple test on three things...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.