473,780 Members | 2,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python types

Hello,

I've read several articles where it's said that Python is weakly typed.
I'm a little surprised. All objects seem to have a perfectly defined
type

Am i wrong?

Regards

Mar 24 '06
13 1627
Grazie ALex, for your comment.

Mar 25 '06 #11
Salvatore a écrit :
Thank's everybody :-)
Here is a type définition I've found on the net which I agree with :

Attribute of a variable which determines the set of the values this
variabe can take and the
operations we can apply on it.


Then - as already pointed by Alex - there is no type in Python, since
there is no variable (even if this term is often improperly used for
bindings) !-)

Ok, let's s/variable/object/g and define some objects and operations:

def myop(obj):
return obj.foo * 2

class Bar(object):
pass

b = Bar()

Can we apply the myop() operation on the object name 'b' is bound to ?

myop(b)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in myop
AttributeError: 'Bar' object has no attribute 'foo'
Well... but wait a minute:
b.foo = []
myop(b)
-> []

Err... Wait another minute:

b2 = Bar()
type(b) is type(b2)
-> True
myop(b2)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in myop
AttributeError: 'Bar' object has no attribute 'foo'

Ok, so even if Python itself declares b and b2 (read: objects that names
b and b2 are bound to) to be of the same type, you cannot apply the
myop() operation on b2...

Also:

del b.foo
myop(b)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in myop
AttributeError: 'Bar' object has no attribute 'foo'
So *sometimes* you can apply myop() to b, and sometimes you can't.

Now if we come back to your original post:
"""
All objects seem to have a perfectly defined
type
"""

"Perfectly defined" ? Really ? Not for your current definition of 'type'
at least !-)

I still mostly agree with the given definition of type. But the fact is
that in Python, the type*s* of an object are not so perfectly defined -
they're mostly implicits, and can vary during the object's lifetime.

Note that it does'nt make Python weakly typed - you cannot perform any
arbitrary operation on a given object, only the operations this object
can support at a given moment. FWIW, if you want a weakly typed
language, take a look at C.
Mar 26 '06 #12
Thank's Bruno.

Mar 26 '06 #13
Dennis Lee Bieber wrote:
On Mon, 27 Mar 2006 01:34:14 +0200, Bruno Desthuilliers
<bd************ *****@free.quel quepart.fr> declaimed the following in
comp.lang.pytho n:

Ok, so even if Python itself declares b and b2 (read: objects that names
b and b2 are bound to) to be of the same type, you cannot apply the
myop() operation on b2...
Try looking at class-based objects as a form of CONTAINER...


ouch, my ears :(
Two
containers can be of the same type, but the contents may be different.

(snip)

This is mostly how OO is implemented in Python (in javascript too FWIW)
- but this is not the semantic of classes/objects in OO. The OO
translation of a type - at least according to the definition proposed by
the OP, and this is a pretty common definition - is a set of attributes
and methods. In most OO languages, this set is fixed for *all* the
instances of a class (subclassing not withstanding - and subclassing is
already a not so trivial problem in type theory).

What I wanted to point out is that, while class-based, Python in, in
this respect, closer to a prototype-based language.

In fact, since the set of attributes and methods of a given object may
change during the object's lifetime, it's type(s) may change too. So
saying that objects in Python "have a perfectly defined type" is perhaps
not really accurate, or at least requires further precisions (well, IMHO)

And I didn't even mention the possibility of changing the value of
anobject.__clas s__ at runtime !-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Mar 27 '06 #14

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

Similar topics

24
2324
by: Matt Feinstein | last post by:
Hi all-- I'm new to Python, and was somewhat taken aback to discover that the core language lacks some basic numerical types (e.g., single-precision float, short integers). I realize that there are extensions that add these types-- But what's the rationale for leaving them out? Have I wandered into a zone in the space/time continuum where people never have to read binary data files? Matt Feinstein
1
2577
by: M.E.Farmer | last post by:
Hello c.l.py!, I have just finished this and decided to share. PySourceColor is a module to convert Python source into colored html. Yes it has been done before, but I like this better:) You can easily define your own colorscheme. example usage: # Highlight PySourceColor.py python PySourceColor.py or # Show help
2
4453
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
1
4914
by: Justin Johnson | last post by:
Hello, I'm trying to build Python 2.5.0 on AIX 5.3 using IBM's compiler (VisualAge C++ Professional / C for AIX Compiler, Version 6). I run configure and make, but makes fails with undefined symbols. See the output from configure and make below. svnadm /svn/build/python-2.5.0>env CC=cc CXX=xlC ./configure --prefix=$base_dir \ checking MACHDEP... aix5
0
323
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 357 open ( +8) / 3745 closed ( +8) / 4102 total (+16) Bugs : 958 open (+19) / 6657 closed ( +9) / 7615 total (+28) RFE : 251 open ( +2) / 280 closed ( +2) / 531 total ( +4) New / Reopened Patches ______________________
4
2489
by: Markus Dahlbokum | last post by:
Hello, I'm trying to link python statically with qt and pyqt. I've tried this in several ways but never succeeded. At the moment the final make runs without errors but I get import errors when accessing pyqt. How can I solve this problem? Markus # installing zipimport hook
0
1179
by: rkmr.em | last post by:
the memory usage of a python app keeps growing in a x86 64 linux continuously, whereas in 32 bit linux this is not the case. Python version in both 32 bit and 64 bit linux - 2.6.24.4-64.fc8 Python 2.5.1 (r251:54863, Oct 30 2007, 13:45:26) i isolated the memory leak problem to a function that uses datetime module extensively. i use datetime.datetime.strptime, datetime.timedelta, datetime.datetime.now methods... i tried to get some info...
0
9474
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,...
0
10306
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10139
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
10075
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,...
1
7485
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
6727
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
5373
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
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
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 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.