Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Goals
When you complete this workshop, you should have a basic understanding of Python
objects.
Tasks
1. Type, ID, and Value. Investigate the identity, type, and value of different objects. If
you want, try using code similar to the course example below. This code may be
found in the file called ws_scr_compare.py): The functionality may be called at the
interactive prompt by importing the module and then calling the function. For
example:
# ws_scr_compare.py
import types
def compare(x, y):
print 'id for x and y =', (id(x), id(y))
if x is y: print 'x and y are the same object'
if x == y: print 'x and y have the same value'
if type(x) is type(y): # in this case, same as using ==
print 'x and y have the same type'
if type(x) is types.IntType:
print 'x is an integer'
else:
print 'x is not an integer'
2. Attributes. Create a list object (e.g. L=[1,2,3]). What attributes are associated with
this object? Hint: try using the dir() function. Which of these attributes are
methods? Which are members? Try the same for a file object.
3. Copies versus Reference. At the Python command line, try some of the examples
from class:
#Immutable objects
>>> a = 1; b = a
>>> a = 2
>>> b
1
# Mutable objects
>>> a = [1, 2, 3]; b = a
>>> a[2] = 4
>>> b
[1, 2, 4]
#Repetition copy
>>> L0 = [1, 2, 3]
>>> L1 = L0 * 2; L1
[1,2,3,1,2,3]
>>> L2 = [L0] * 2; L2
[[1,2,3],[1,2,3]]
>>> L0[1] = 0
>>> L1
[1, 2, 3, 1, 2, 3]
>>> L2
[[1, 0, 3], [1, 0, 3]]
>>> L2[0][1] = 2
>>> L2
[[1, 2, 3], [1, 2, 3]]
Goals
When you complete this workshop, you should have a basic understanding of Python
objects.
Tasks
1. Type, ID, and Value. Investigate the identity, type, and value of different objects. If
you want, try using code similar to the course example below. This code may be
found in the file called ws_scr_compare.py): The functionality may be called at the
interactive prompt by importing the module and then calling the function. For
example:
# ws_scr_compare.py
import types
def compare(x, y):
print 'id for x and y =', (id(x), id(y))
if x is y: print 'x and y are the same object'
if x == y: print 'x and y have the same value'
if type(x) is type(y): # in this case, same as using ==
print 'x and y have the same type'
if type(x) is types.IntType:
print 'x is an integer'
else:
print 'x is not an integer'
2. Attributes. Create a list object (e.g. L=[1,2,3]). What attributes are associated with
this object? Hint: try using the dir() function. Which of these attributes are
methods? Which are members? Try the same for a file object.
c:\temp>abaqus python
Python 2.7.15 for Abaqus 2020
[MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more
information.
>>> L=[1,2,3]
>>> dir(L)
['__add__', '__class__', '__contains__', '__delattr__',
'__delitem__', '__delslice__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__',
'__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__',
'__init__', '__iter__', '__le__', '__len__', '__lt__',
'__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__reversed__', '__rmul__', '__setattr__',
'__setitem__', '__setslice__', '__sizeof__', '__str__',
'__subclasshook__', 'append', 'count', 'extend', 'index',
'insert', 'pop', 'remove', 'reverse', 'sort']
>>> f=open('workfile.txt','w')
>>> dir(f)
['__class__', '__delattr__', '__doc__', '__enter__',
'__exit__', '__format__', '__getattribute__', '__hash__',
'__init__', '__iter__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', 'close', 'closed', 'encoding',
'errors', 'fileno', 'flush', 'isatty', 'mode', 'name',
'newlines', 'next', 'read', 'readinto', 'readline',
'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write',
'writelines', 'xreadlines']
>>>
3. Copies versus Reference. At the Python command line, try some of the examples
from the course:
#Immutable objects
>>> a = 1; b = a
>>> a = 2
>>> b
1
>>> a=1
>>> b=a
>>> id(a)
55139304L
>>> id(b)
55139304L
>>> a=2
>>> id(a)
55139280L
>>> id(b)
55139304L
>>> id(2)
55139280L
>>> id(1)
55139304L
>>>
Note :
a=2 creates a new 2 (not used before) and the id is used for a. The old id stays with b
# Mutable objects
>>> a = [1, 2, 3]; b = a
>>> a[2] = 4
>>> b
[1, 2, 4]
>>> a = b = [1,2,3]
>>> c = a[:]
>>> b.append(4)
>>> id(c)
59580936L
>>> id(c[0])
55139304L
>>> id(a)
59579208L
>>> id(b)
59579208L
>>> a
[1, 2, 3, 4]
>>> b
[1, 2, 3, 4]
>>> c
[1, 2, 3]
>>>
Note : Only the address is shared between a and b. The whole array is not copied!
a[:] is a slice operation that makes a top-level copy of the object a .
#Repetition copy
>>> L0 = [1, 2, 3]
>>> L1 = L0 * 2; L1
[1,2,3,1,2,3]
>>> L2 = [L0] * 2; L2
[[1,2,3],[1,2,3]]
>>> L0[1] = 0
>>> L1
[1, 2, 3, 1, 2, 3]
>>> L2
[[1, 0, 3], [1, 0, 3]]
>>> L2[0][1] = 2
>>> L2
[[1, 2, 3], [1, 2, 3]]
L2=[L0]*2 Same as
[L0] + [L0] refers twice L0