473,769 Members | 2,382 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 #1
13 1626
I think it means that names, not objects, are weakly typed. So you can
have:
a = 4
a = 'hello'

and there is no problem. The name 'a' doesn't have any type associated
with it. This contrasts with strongly typed language like C where you
declare the type of the name (variable) and the compiler objects at
compile time if you attempt to assign a value of a different type.

Mar 24 '06 #2
Steve M schrieb:
I think it means that names, not objects, are weakly typed. So you can
have:
a = 4
a = 'hello'

and there is no problem. The name 'a' doesn't have any type associated
with it. This contrasts with strongly typed language like C where you
declare the type of the name (variable) and the compiler objects at
compile time if you attempt to assign a value of a different type.


void foo() {
int *c = "hello weakly typed C!";
}
192:/tmp deets$ gcc -c test.c
test.c: In function `foo':
test.c:5: warning: initialization from incompatible pointer type

So I wouldn't call C strongly typed....

The distinction is usually made on two axis: strong-weak and
static-dynamic. Python is a strong-typed, dynamic language. JAVA is
strong-typed static. And PHP is weakly-typed dynamic.

Regards,

Diez
Mar 24 '06 #3
"Salvatore" <sa************ *@wanadoo.fr> writes:
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?


You're right. All Python values are strongly typed; they don't, in
general, change their type; and operations between values of
mismatched types are not allowed.

A more recent distinction than "strong" vs "weak" typing, is "dynamic"
vs "static" typing.

"Typing: Strong vs. Weak, Static vs. Dynamic"
<URL:http://www.artima.com/weblogs/viewpost.jsp?th read=7590>

--
\ "I would rather be exposed to the inconveniences attending too |
`\ much liberty than those attending too small a degree of it." |
_o__) -- Thomas Jefferson |
Ben Finney

Mar 24 '06 #4
Salvatore wrote:
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


Hoping to head off another debate:
http://wiki.python.org/moin/StrongVsWeakTyping

STeVe
Mar 24 '06 #5

Salvatore wrote:
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?

No, you're right. It seems like a lot of people conflate weak vs.
strong typing and static vs. dynamic typing. Back when I was in CS
classes in the early 1990s, the distinction was pretty universal, but
in recent years it seems like more people are wrongly assuming that
strong typing and static typing go hand in hand.

C, C++: Weakly, statically typed
Java, ML: Strongly, statically typed.
VB, tcl (kind of): Weakly, dynamically typed
Smalltalk, Python: Strongly, dynamically typed

Note that both axes are continuums. C++ is mostly statically typed,
but it's got rtti and other runtime typing facilities. Java is pretty
strongly typed, but not to the degree that ML is.

Mar 24 '06 #6
Salvatore a écrit :
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?


Depends on your definition of 'type'.

Mar 24 '06 #7
Salvatore wrote:
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

Aye, the other posters are right about you being right. This is just one
of the great mass confusions in programming (sadly, there are a lot of
them these days).
Mar 25 '06 #8
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.

Mar 25 '06 #9
Salvatore <sa************ *@wanadoo.fr> wrote:
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.


Hmmm -- that doesn't work very well for languages in which "a variable"
is just "a name", because we cannot apply any operations at all on THE
NAME -- we apply operations on the OBJECT to which the name refer. This
issue arises with both languages where names "can take" any object, like
Python, and ones where the compiler infers the subset (type) of objects
that each name "can take", like Boo.

Moreover, asserting that 'type' is an attribute of a variable means, for
example, that a function's return-value, not being a variable, has no
type -- that really makes no sense. And similarly for other
expressions; e.g., consider, in Java, something like...:

( (Fooable) zip() ).getFoo() + ( (Barable) zop() ).getBar()

no variables in sight, yet a lot of types in Java's normal sense -- the
types of whatever objects zip() and zop() return, the (Fooable and
Barable, respectively) types after the cast, the type of whatever getFoo
and getBar return, and the type of their sum...

Saying that an _object_ has a type thus makes more sense (even in Java
and similar languages) than considering type to be an "attribute of a
variable" -- *in addition* to objects, which have types, other language
constructs, depending on the language, may or may not be imbued with
type-constraints, be that declaratively (as, say, in Java), by compiler
inference (as, say, in Boo), or by other means yet such as stropping
(e.g., in Perl, you can tell from just looking at the name whether a
variable refers to a scalar, $something, an array, @something, or a
hash, %something -- in Python, Java or Boo you can't tell from just the
name, but rather must find a declaration [Java], assignment [Python] or
use [Boo] to let you read or infer the "scalar vs array" type issue).
Alex
Mar 25 '06 #10

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

Similar topics

24
2323
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
4912
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
2488
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
9586
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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
10210
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
10043
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...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
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
7406
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
5298
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...
1
3956
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.