Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 543707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:34:44+00:00 2026-05-13T10:34:44+00:00

class a(object): w=’www’ def __init__(self): for i in self.keys(): print i def __iter__(self): for

  • 0
class a(object):
    w='www'
    def __init__(self):
        for i in self.keys():
            print i
    def __iter__(self):
            for k in self.keys():
                yield k

a() # why is there an error here?

Thanks.


Edit: The following class also doesn’t extend any class;
why it can use keys?

class DictMixin:
    # Mixin defining all dictionary methods for classes that already have
    # a minimum dictionary interface including getitem, setitem, delitem,
    # and keys. Without knowledge of the subclass constructor, the mixin
    # does not define __init__() or copy().  In addition to the four base
    # methods, progressively more efficiency comes with defining
    # __contains__(), __iter__(), and iteritems().

    # second level definitions support higher levels
    def __iter__(self):
        for k in self.keys():
            yield k
    def has_key(self, key):
        try:
            value = self[key]
        except KeyError:
            return False
        return True
    def __contains__(self, key):
        return self.has_key(key)

    # third level takes advantage of second level definitions
    def iteritems(self):
        for k in self:
            yield (k, self[k])
    def iterkeys(self):
        return self.__iter__()

    # fourth level uses definitions from lower levels
    def itervalues(self):
        for _, v in self.iteritems():
            yield v
    def values(self):
        return [v for _, v in self.iteritems()]
    def items(self):
        return list(self.iteritems())
    def clear(self):
        for key in self.keys():
            del self[key]
    def setdefault(self, key, default=None):
        try:
            return self[key]
        except KeyError:
            self[key] = default
        return default
    def pop(self, key, *args):
        if len(args) > 1:
            raise TypeError, "pop expected at most 2 arguments, got "\
                              + repr(1 + len(args))
        try:
            value = self[key]
        except KeyError:
            if args:
                return args[0]
            raise
        del self[key]
        return value
    def popitem(self):
        try:
            k, v = self.iteritems().next()
        except StopIteration:
            raise KeyError, 'container is empty'
        del self[k]
        return (k, v)
    def update(self, other=None, **kwargs):
        # Make progressively weaker assumptions about "other"
        if other is None:
            pass
        elif hasattr(other, 'iteritems'):  # iteritems saves memory and lookups
            for k, v in other.iteritems():
                self[k] = v
        elif hasattr(other, 'keys'):
            for k in other.keys():
                self[k] = other[k]
        else:
            for k, v in other:
                self[k] = v
        if kwargs:
            self.update(kwargs)
    def get(self, key, default=None):
        try:
            return self[key]
        except KeyError:
            return default
    def __repr__(self):
        return repr(dict(self.iteritems()))
    def __cmp__(self, other):
        if other is None:
            return 1
        if isinstance(other, DictMixin):
            other = dict(other.iteritems())
        return cmp(dict(self.iteritems()), other)
    def __len__(self):
        return len(self.keys())
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:34:44+00:00Added an answer on May 13, 2026 at 10:34 am

    Why would you expect it to have keys? You didn’t define such a method in your class. Did you intend to inherit from a dictionary?

    To do that declare class a(dict)

    Or maybe you meant a.__dict__.keys()?

    As for the large snippet you’ve posted in the update, read the comment above the class again:

     # Mixin defining all dictionary methods for classes that already have
     # a minimum dictionary interface including getitem, setitem, delitem,
     # and keys
    

    Note that “already have … keys” part.

    The DictMixin class comes from the UserDict module, which says:

    class UserDict.DictMixin Mixin
    defining all dictionary methods for
    classes that already have a minimum
    dictionary interface including
    getitem(), setitem(), delitem(), and keys().

    This mixin should be used as a
    superclass. Adding each of the above
    methods adds progressively more
    functionality. For instance, defining
    all but delitem() will preclude
    only pop() and popitem() from the full
    interface.

    In addition to the four base methods,
    progressively more efficiency comes
    with defining contains(),
    iter(), and iteritems().

    Since the mixin has no knowledge of
    the subclass constructor, it does not
    define init() or copy().

    Starting with Python version 2.6, it
    is recommended to use
    collections.MutableMapping instead of
    DictMixin.

    Note the recommendation in the last part – use collections.MutableMapping instead.

    To iterate over attributes of an object:

    class A(object):
        def __init__(self):
            self.myinstatt1 = 'one'
            self.myinstatt2 = 'two'
        def mymethod(self):
            pass
    
    a = A()
    for attr, value in a.__dict__.iteritems():
        print attr, value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 275k
  • Answers 275k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I usually write my own, but via Jack Ganssle's Embedded… May 13, 2026 at 2:35 pm
  • Editorial Team
    Editorial Team added an answer Unmanaged code rarely needs a lot of help from C#… May 13, 2026 at 2:35 pm
  • Editorial Team
    Editorial Team added an answer The formBackingObject() method is being called when the user makes… May 13, 2026 at 2:35 pm

Related Questions

I have been going crazy with binding a combobox to an enum typed property
Say I am declaring a class C and a few of the declarations are
.NET allows to extend XSLT by using the so called extension object. Very handy
I'm writing the RenderContents() method of my ASP.NET server control. The method uses an
I'm accessing a data context object that is auto-generated by using LINQ to SQL.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.