473,378 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Dive into Python 5.5

Hi,

class UserDict:
def __init__(self, dict=None):
self.data = {}
if dict is not None: self.update(dict)

I just don't understant this code, as it is not also mention in the
book. the update is a method of a dict right? in my understanding the
last statement should be self.data.update(dict).

someone please explain to me what happen where?

Thanks
james

Jun 13 '07 #1
3 1567
james_027 <ca********@gmail.comwrites:
class UserDict:
def __init__(self, dict=None):
self.data = {}
if dict is not None: self.update(dict)
The code confusingly shadows the builtin 'dict' type with a
poorly-chosen parameter name. See if this makes things less
confusing::

class UserDict:
def __init__(self, from=None):
self.data = {}
if from is not None:
self.update(from)
I just don't understant this code, as it is not also mention in the
book. the update is a method of a dict right? in my understanding
the last statement should be self.data.update(dict).
As you point out, the 'update' method isn't defined, and the class
inherits from no base classes, so this class as written will fail in
the __init__ method when the 'self.update' attribute cannot be found.

What should be happening is that the class should inherit from the
Python dict type:

class UserDict(dict):
# ...

That way, the 'update' method will be inherited from the 'dict.update'
method, and likewise for all the other behaviour expected from a dict
type.

--
\ "Madness is rare in individuals, but in groups, parties, |
`\ nations and ages it is the rule." -- Friedrich Nietzsche |
_o__) |
Ben Finney
Jun 13 '07 #2
class UserDict:
def __init__(self, dict=None):
self.data = {}
if dict is not None: self.update(dict)

I just don't understant this code, as it is not also mention in the
book. the update is a method of a dict right? in my understanding the
last statement should be self.data.update(dict).

someone please explain to me what happen where?
You are right, this code will not work, and your suggestion is a reasonable one.

HTH,
Daniel
Jun 13 '07 #3
On Jun 13, 2:40 am, james_027 <cai.hai...@gmail.comwrote:
Hi,

class UserDict:
def __init__(self, dict=None):
self.data = {}
if dict is not None: self.update(dict)

I just don't understant this code, as it is not also mention in the
book. the update is a method of a dict right? in my understanding the
last statement should be self.data.update(dict).

someone please explain to me what happen where?

Thanks
james
This is what "Dive" says:

---
To explore this further, let's look at the UserDict
class in the UserDict module...In particular, it's stored in the lib
directory in your Python installation.
---

So you can actually locate the file UserDict.py on your computer and
look at the code. If you don't want to do that, then the following is
an explanation of what's going on with that code.

Suppose you have a class like this:
class Dog(object):
def __init__(self):
self.update()

When __init__ executes, the only line in __init__ says go look in self
for the method update() and execute it. That means the Dog class
probably has at least one additional method:

class Dog(object):
def __init__(self):
self.update()

def update(self):
print "hello"

So if you wrote:

d = Dog()

the output would be:

hello

Ok, now suppose you add a line to __init__:

class Dog(object):
def __init__(self):
self.data = {}
self.update()

def update(self):
print "hello"

Does the new line in __init__ affect the line self.update() in any
way? Is there necessarily any connection between self.data and
update()? No.

However, if you look at the actual code for UserDict, you can see that
inside the method update(), items are added to self.data, so there is
a connection.

Then the question is: why didn't the person who wrote the class just
do the following in __init__:

self.data.update(dict)

Well, as it turns out, adding items to self.data is not that straight
forward. self.data is a dict type and dict types have an update()
method that requires another dict as an argument. But the 'dict'
parameter variable in __init__ could be sent a dict type or it could
be sent another instance of UserDict. So, the code to add items to
self.data got complicated enough that the writer of the class decided
to move the code into its own method. Note that just because the
parameter variable is named 'dict' does not mean the argument sent to
the function is a dict type. For instance, you could write this:

dict = "hello world"

As you can see, the variable named 'dict' does not refer to a dict
type. Using a python type as a variable name is a horrible and
confusing thing to do, so don't do it in your code.

In addition, the writer of the class wanted to provide an update()
method for the UserDict class, which could be called at any time, so
instead of having to write the same code in two places: once in
__init__ to initialize an instance with a given dict and a second time
inside the update() method, the programmer wrote the code once in its
own method and then called the method from __init__.


Jun 13 '07 #4

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

Similar topics

0
by: Luis P. Mendes | last post by:
Hi, do you know if is there any 'Dive into Python' equivalent for the java language? DiP is the best I've seen and I would need to learn some basics of Java and also ways to interact between...
6
by: Franz Mueller | last post by:
Hi, which of the following books would you recommend: "Dive into Python" or "Beginning Python: From Novice to Professional"? I'm an experienced C++-programmer who wants to take a look at...
2
by: Franz Mueller | last post by:
Hi there, is there a nicer looking version of the "Dive into Python" PDF? The one from diveintopython.org looks horrible, much worse than the web-version; also the images aren't where they...
5
by: Casey Hawthorne | last post by:
Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python" The and-or conditional expression trick from page 41 of "Dive into Python"...
3
by: hanumizzle | last post by:
I find Dive Into Python generally an excellent text, and I am not surprised to see people recommending it...but I have noticed a few errors already: ...
1
by: Ben Edwards (lists) | last post by:
I have been going through Dive into Python which up to now has been excellent. I am now working through Chapter 9, XML Processing. I am 9 pages in (p182) in the 'Parsing XML section. The...
2
by: Fred C. Dobbs | last post by:
I feel like an idiot. I'm going thru "Dive Into Python" and running the first program - odbchelper.py My output is "pwd=secret;database=master;uid=sa;server=mpilgrim" which has all the...
51
by: erikcw | last post by:
DiveIntoPython.org was the first book I read on python, and I really got a lot out of it. I need to start learning Java (to maintain a project I've inherited), and was wondering if anyone knew of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.