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? 41 1614
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.
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."
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
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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
...
|
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...
|
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...
|
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...
|
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...
| |
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,...
|
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,...
|
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...
|
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...
|
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
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
| |
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...
| |