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

  • SEARCH
  • Home
  • 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 517757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:52:02+00:00 2026-05-13T07:52:02+00:00

the next is my code: class foo: def __init__(self): self.a = a def __getattr__(self,x,defalut):

  • 0

the next is my code:

class foo:
    def __init__(self):
        self.a = "a"
    def __getattr__(self,x,defalut):
        if x in self:
            return x
        else:return defalut

a=foo()
print getattr(a,'b','sss')

i know the __getattr__ must be 2 argument,but i want to get a default attribute if the attribute is no being.

how can i get it, thanks


and

i found if defined __setattr__,my next code is also can’t run

class foo:
    def __init__(self):
        self.a={}
    def __setattr__(self,name,value):
            self.a[name]=value

a=foo()#error ,why

hi alex,
i changed your example:

class foo(object):
    def __init__(self):
        self.a = {'a': 'boh'}
    def __getattr__(self, x):
        if x in self.a:
            return self.a[x]
        raise AttributeError

a=foo()
print getattr(a,'a','sss')

it print {‘a’: ‘boh’},not ‘boh’
i think it will print self.a not self.a[‘a’], This is obviously not want to see

why ,and Is there any way to avoid it

  • 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-13T07:52:02+00:00Added an answer on May 13, 2026 at 7:52 am

    Your problem number one: you’re defining an old-style class (we know you’re on Python 2.something, even though you don’t tell us, because you’re using print as a keyword;-). In Python 2:

    class foo:
    

    means you’re defining an old-style, aka legacy, class, whose behavior can be rather quirky at times. Never do that — there’s no good reason! The old-style classes exist only for compatibility with old legacy code that relies on their quirks (and were finally abolished in Python 3). Use new style classes instead:

    class foo(object):
    

    and then the check if x in self: will not cause a recursive __getattr__ call. It will however cause a failure anyway, because your class does not define a __contains__ method and therefore you cannot check if x is contained in an instance of that class.

    If what you’re trying to do is whether x is defined in the instance dict of self, don’t bother: __getattr__ doesn’t even get called in that case — it’s only called when the attribute is not otherwise found in self.

    To support three-arguments calls to the getattr built-in, just raise AttributeError in your __getattr__ method if necessary (just as would happen if you had no __getattr__ method at all), and the built-in will do its job (it’s the built-in‘s job to intercept such cases and return the default if provided). That’s the reason one never ever calls special methods such as __getattr__ directly but rather uses built-ins and operators which internally call them — the built-ins and operators provide substantial added value.

    So to give an example which makes somewhat sense:

    class foo(object):
        def __init__(self):
            self.blah = {'a': 'boh'}
        def __getattr__(self, x):
            if x in self.blah:
                return self.blah[x]
            raise AttributeError
    
    a=foo()
    print getattr(a,'b','sss')
    

    This prints sss, as desired.

    If you add a __setattr__ method, that one intercepts every attempt to set attributes on self — including self.blah = whatever. So — when you need to bypass the very __setattr__ you’re defining — you must use a different approach. For example:

    class foo(object):
        def __init__(self):
            self.__dict__['blah'] = {}
        def __setattr__(self, name, value):
            self.blah[name] = value
        def __getattr__(self, x):
            if x in self.blah:
                return self.blah[x]
            raise AttributeError
    
    a=foo()
    print getattr(a,'b','sss')
    

    This also prints sss. Instead of

            self.__dict__['blah'] = {}
    

    you could also use

            object.__setattr__(self, 'blah', {})
    

    Such “upcalls to the superclass’s implementation” (which you could also obtain via the super built-in) are one of the rare exceptions to the rules “don’t call special methods directly, call the built-in or use the operator instead” — here, you want to specifically bypass the normal behavior, so the explicit special-method call is a possibility.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • 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 Yes. The one which you gave in the main MXML… May 14, 2026 at 8:34 pm
  • Editorial Team
    Editorial Team added an answer It seams that the Oracle Installer will sometimes forget about… May 14, 2026 at 8:34 pm
  • Editorial Team
    Editorial Team added an answer This isn't in the documentation anywhere, but it appears that… May 14, 2026 at 8:34 pm

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.