Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

i am developing in Python 3, i have a method with an argument of my class Color, however when i try to use a method:

def __init__(self, name, color=Color.Color("none"), style="solid", width="1px"):
    self.__name = name
    self.__style = style
    self.__width = width
    if color.get_name() == "none":
        color.color_by_name('Black')

This is the error:

File ................, line 10, in __init__
if color.get_name() == "none":
AttributeError: 'str' object has no attribute 'get_name'
share|improve this question
    
Seems like Color.Color() isn't a class, but a method that returns a string. Am i right? – aIKid Oct 23 '13 at 16:32
1  
Can you please show your Color Class? – aIKid Oct 23 '13 at 16:33
2  
What's the rest of the traceback? For some reason, color is a string. Perhaps the default value is a string, or the argument passed in some specific case is a string. – delnan Oct 23 '13 at 16:34
    
@delnan's right, where you're instantiating this nameless class, the argument you've given for color was probably confused with the one for name or style. – Brian Cain Oct 23 '13 at 18:39

3 Answers 3

Sounds like you want to either accept a Color object, or a string. You can duck-type, or explicitly check:

duck-type:

def __init__(self, name, color=Color.Color("Black"), style="solid", width="1px"):
    # Note: Changed default to 'Black', since there is no color 'none'
    # This makes it a lot simpler.
    self.__name = name
    self.__style = style
    self.__width = width
    try: 
        color.get_name()
    except AttributeError:
        color = Color.Color(color)

explicit check:

def __init__(self, name, color=Color.Color("Black"), style="solid", width="1px"):
    self.__name = name
    self.__style = style
    self.__width = width
    if not isinstance(color, Color.Color):
        color = Color.Color(color)

either way works; which one is easier depends on how definite your check is (i.e. do you have other classes like color which need separate isinstance checks? is there a convenient duck type check that catches all failures).

share|improve this answer
1  
You should be a little clearer on what you're catching in the Except. Here it would be except AttributeError or something, since you don't want to ignore other errors within the get_name function (like a database search error or whatever) – RodericDay Oct 23 '13 at 20:28

You are using the wrong sentinel value when None is great for that. Try:

def __init__(self, name, color=None, style="solid", width="1px"):
    self.__name = name
    self.__style = style
    self.__width = width
    if color is None:
        self.color = "Black"
    else
        self.color = color

But there is a confusion between class attributes which will only be cleared up by showing the full class. The odd bit is having a class that has an instance of its own type as an attribute.

And you shouldn't use double underscore attribute names like __width unless you really have a need to. Many newcomers to Python think "that's the way to simulate private members" when the language really does not provide a private storage class.

share|improve this answer

Probably this error caused by wrong data passed.

To avoid this You can check passed arguments.

If Your code rely on exact class instances You should check whether passed argument is instance of this class:

assert isinstance(color, Color), "color should be an instance of Color"

If You don't care about instance class but want to be sure that this instance has attribute get_color check whether instance has attribute

if not hasattr(color, 'get_color'):
    color=Color.Color("none")
if color.get_name() == "none":
    color.color_by_name('Black')
share|improve this answer
    
This doesn't answer the question. It's only an advice to troubleshoot the problem. – aIKid Oct 23 '13 at 16:38
    
question was How do fix objects of wrong type in Python? this will fix objects of wrong type issue – oleg Oct 23 '13 at 16:40
    
This also doesn't address the issue of passing an object as a default value. – Andy Hayden Oct 23 '13 at 17:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.