473,543 Members | 2,446 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 #1
41 1573
process wrote:
What is not an object in Python?
Everything that is not part of Python's syntax is an object, including
all string and number types, classes, metaclasses, functions, models,
code and more. It's technically not possible to have something like e.g.
an int that isn't an object.
Why isn't len implemented as a str.len and list.len method instead of
a len(list) function?
Because readability is more important than purity. By the way len(egg)
is just syntactic sugar for egg.__len__() with some extra checks.

Sep 28 '08 #2
Lie
On Sep 29, 1:29*am, process <circularf...@g mail.comwrote:
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?
A question like this is often answered by another (rhetorical)
question: "What is Object Oriented actually?"

The answer to that is generally: "Python is not Java."
Sep 28 '08 #3
process wrote:
I have heard some criticism about Python, that it is not fully object-
oriented.
Feel free to ignore it if you wish.
What is not an object in Python?
Depends on what you mean by 'in'. Python is a language for defining and
manipulating information objects. Code 'in' Python is not usually a
maniputed object, though is can be (by eval, exec, and compile, for
instance). Names in code that are only used to directly access objects
typically are also, typically, not objects themselves.
Why isn't len implemented as a str.len and list.len method instead of
a len(list) function?

Partly history and partly practicality. Len is implemented as .__len__
;-). The len function is one, __len__ methods are many. If you want to
pass an argument to a function, passing len is easier that passing
operator.attrge tter('__len__') . Passing '__len__' (or 'len') would be
easy, but using len is easier than using getattr(ob,'__l en__').

tjr


Sep 28 '08 #4
Terry Reedy:
Partly history and partly practicality. Len is implemented as .__len__
;-). The len function is one, __len__ methods are many. If you want to
pass an argument to a function, passing len is easier that passing
operator.attrge tter('__len__') . Passing '__len__' (or 'len') would be
easy, but using len is easier than using getattr(ob,'__l en__').
A simple example may help:
>>seq = ["aaaa", "bb", "c", "ddd"]
seq2 = [[1,1,1,1], [2,2], [3], [4,4,4]]
sorted(seq, key=len)
['c', 'bb', 'ddd', 'aaaa']
>>sorted(seq2 , key=len)
[[3], [2, 2], [4, 4, 4], [1, 1, 1, 1]]
>>sorted(seq, key=str.__len__ )
['c', 'bb', 'ddd', 'aaaa']
>>sorted(seq2 , key=str.__len__ )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor '__len__' requires a 'str' object but received a
'list'
>>from operator import attrgetter
sorted(seq, key=attrgetter( "__len__"))
['aaaa', 'bb', 'c', 'ddd']
>>sorted(seq2 , key=attrgetter( "__len__"))
[[1, 1, 1, 1], [2, 2], [3], [4, 4, 4]]

Bye,
bearophile
Sep 28 '08 #5
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.

What is not an object in Python?
Parts of the syntax aren't objects. e.g. "=" or ":" aren't objects.

Unlike in some less fully OO-languages (e.g. Java or C++), classes,
functions, and many other "built-in language features" are objects in
Python. You can do things like return functions just like any other
object, rather than having to do it indirectly through references or
some such:
>>def add_n(x):
.... def rv(y):
.... return y + x
.... return rv
....
>>add_2 = add_n(2)
add_3 = add_n(3)

print add_2(6)
8
>>print add_2(10)
12
>>print add_3(6)
9
Why isn't len implemented as a str.len and list.len method instead of
a len(list) function?
FWIW, it is implemented as a str.__len__ method (and list.__len__
method); the len() function just uses those internally. Java and C++
have similar shortcuts for, say, "+" or "-". But Python allows you to
call all the operators as methods if you want:
>>1+2
3
>>(1).__add__(2 )
3
>>a_list = [ "a", "b", "c" ]
len(a_list)
3
>>a_list.__len_ _()
3
And, of course, the presence of the len() shortcut doesn't alter the
OO-nature of the language any more than the presence of the + operator
does in any OO language. Derived classes' __len__ operators are
called correctly by len():
>>class list_that_lies( list):
.... def __len__(self):
.... return 2
....
>>bad_list=list _that_lies([1,2])
print bad_list
[1, 2]
>>len(bad_lis t)
2
>>bad_list.appe nd(3)
print bad_list
[1, 2, 3]
>>len(bad_lis t)
2

Sep 28 '08 #6
2008/9/28 process <ci**********@g mail.com>:
I have heard some criticism about Python, that it is not fully object-
oriented.
Why is that a criticism? OO is a tool, not a religion (ok, ok, OO
*should be* a tool, not a religion). Is it a criticism of a hammer
that it is not a screwdriver? Or do you pick the tool that does the
job in hand most effectively?

--
Tim Rowe
Sep 28 '08 #7
On Sep 28, 2:29*pm, process <circularf...@g mail.comwrote:
Why isn't len implemented as a str.len and list.len method instead of
a len(list) function?
Although len() is spelled like a function call, in spirit it's an
operator, and it behaves like any other operator in Python.

Never mind why len() is an operator and not a method in Python, the
point is, just as operators like + doesn't make a language less object-
oriented (C++ would be very surprised to find out that it's not OO),
neither do operator functions like len().

Having said that, I encourage you to understanda a language for what
it is, not for whatever computer science buzzword labels it has.
Carl Banks
Sep 28 '08 #8
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 )".

George
Sep 28 '08 #9
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). Methods that apply
to one class (or just couple) are called as non-special methods. I am
not sure why del is a statement rather than a function -- perhaps just
because there is no return value (other than the default None).

tjr

Sep 29 '08 #10

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

Similar topics

2
3634
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
18822
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...
54
6508
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...
28
3245
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...
7
2380
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...
2
1403
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...
137
6941
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...
21
1582
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...
4
1307
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...
0
7402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7347
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7590
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7344
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...
0
5883
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...
0
4890
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...
0
3388
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...
1
1814
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
1
963
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.