Sei sulla pagina 1di 11

Workshop 4

Investigating Python Objects

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:

>>> import ws_scr_compare


>>> ws_scr_compare.compare(1., 2)

# 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.

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


W4.2

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]

# Empty slice copy


>>> a = b = [1,2,3]
>>> c = a[:]
>>> b.append(4)
>>> a
[1, 2, 3, 4]
>>> c
[1, 2, 3]

#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]]

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


W4.3

# Explicit copy methods


>>> import copy
>>> a = [[1],[2],[3]]
>>> b = copy.deepcopy(a)
>>> c = copy.copy(a)
>>> a[0][0] = 0
>>> b
[[1], [2], [3]]
>>> c
[[0], [2], [3]]

Can you explain what is happening in each case?

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


Workshop 4 Answers
Investigating Python Objects

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:

>>> import ws_scr_compare


>>> ws_scr_compare.compare(1., 2)

# 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.

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


WA4.2

Run in a console window:

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']
>>>

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


WA4.3

More advanced, to format print attributes in list object use:


for item in dir(L):
print "%20s %80s %20s"%(item,getattr(L,item),type(getattr(L,item)))

Screen dump from a console window:

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


WA4.4

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

This will look similar to below when run in a console window:

>>> 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

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


WA4.5

# Mutable objects
>>> a = [1, 2, 3]; b = a
>>> a[2] = 4
>>> b
[1, 2, 4]

Run in a console window:

>>> a = [1, 2, 3]; b = a


>>> id(a[2])
55139256L
>>> id(b[2])
55139256L
>>> a[2]=4
>>> id(a[2])
55139232L
>>> id(b[2])
55139232L
>>> a[2]
4
>>> b[2]
4
>>>

Note : Address is shared on b=a

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


WA4.6

# Empty slice copy


>>> a = b = [1,2,3]
>>> c = a[:]
>>> b.append(4)
>>> a
[1, 2, 3, 4]
>>> c
[1, 2, 3]

Run in a console window:

>>> 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 .

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


WA4.7

#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]]

Screen dump from a console window:

L1=L0*2 Same as [1, 2, 3] + [1, 2, 3]


copies L0 twice

L2=[L0]*2 Same as
[L0] + [L0] refers twice L0

L0[1]=0 changes the reference in L0[1]


to point on a memory location with '0'

L2[0][1] refers to same loc as L0[1]

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


WA4.8

# Explicit copy methods


>>> import copy
>>> a = [[1],[2],[3]]
>>> b = copy.deepcopy(a)
>>> c = copy.copy(a)
>>> a[0][0] = 0
>>> b
[[1], [2], [3]]
>>> c
[[0], [2], [3]]

Can you explain what is happening in each case?

Shallow copy copy.copy(a):


Creates a new object but populates it with references to the items contained in the
original. Like the empty slice copy previously used

Deep copy copy.deepcopy(a):


Creates a new object and recursively copies all of the objects that it contains.

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting

Potrebbero piacerti anche