how do I make a class return as Namespace(filter='b', list="['hi','by']")

class NameSpace(object):
    """Mock for argparse's NameSpace
    """
    def __init__(self, lst=None, flter=None):
        super(NameSpace, self).__init__()
        self._filter = flter
        self._lst = lst

Is it possible what I am thinking, i guess we have to do something using special method call ?

Edited 3 Years Ago by krystosan: forgot to title properly

i mean object that returns of the type <class 'argparse.Namespace'>

but dont have to use argparse.. just return Namespace(filter='b', list="['hi','by']") as output with filter and list passed at the time of object creation ...

Edited 3 Years Ago by krystosan

Why don't you simply call Namespace's constructor ?

import argparse
obj = argparse.Namespace(filter='b', list="['hi','by']")

Edited 3 Years Ago by Gribouillis

this is what gives me exactly what i want, the tip was enough.

class NameSpace(object):
    """Mock for argparse's NameSpace
    """
    def __init__(self, lst=None, flter=None):
        super(NameSpace, self).__init__()
        self.filter = flter
        self.lst = lst

    def __repr__(self):
        return 'NameSpace(filter=self.filter, lst = self.lst)'

args = NameSpace(lst='["Hi","By"]', flter='B')
print args, type(args), args.lst, args.filter

However I wanted to know if this is equivalent to say mocking a argparse.Namespace behaviour too?

In python 2.7, the Namespace class is defined with this code

class _AttributeHolder(object):
    """Abstract base class that provides __repr__.

    The __repr__ method returns a string in the format::
        ClassName(attr=name, attr=name, ...)
    The attributes are determined either by a class-level attribute,
    '_kwarg_names', or by inspecting the instance __dict__.
    """

    def __repr__(self):
        type_name = type(self).__name__
        arg_strings = []
        for arg in self._get_args():
            arg_strings.append(repr(arg))
        for name, value in self._get_kwargs():
            arg_strings.append('%s=%r' % (name, value))
        return '%s(%s)' % (type_name, ', '.join(arg_strings))

    def _get_kwargs(self):
        return sorted(self.__dict__.items())

    def _get_args(self):
        return []

class Namespace(_AttributeHolder):
    """Simple object for storing attributes.

    Implements equality by attribute names and values, and provides a simple
    string representation.
    """

    def __init__(self, **kwargs):
        for name in kwargs:
            setattr(self, name, kwargs[name])

    __hash__ = None

    def __eq__(self, other):
        return vars(self) == vars(other)

    def __ne__(self, other):
        return not (self == other)

    def __contains__(self, key):
        return key in self.__dict__

Use this code in your program to mock Namespace

hmm, I looked at it in the argparse module. However is it incorrect to mock the way I did ?

This question has already been answered. Start a new discussion instead.