pythonfactory-boy

Is passing too many arguments to the constructor considered an anti-pattern?


I am considering using the factory_boy library for API testing. An example from the documentation is:

class UserFactory(factory.Factory):
    class Meta:
        model = base.User

    first_name = "John"
    last_name = "Doe"

For this to work, we need first_name, last_name, etc to be passed as parameters to the __init__() method of the base.User() class. However, if you have many parameters this leads to something like:

class User(object):

    GENDER_MALE = 'mr'
    GENDER_FEMALE = 'ms'

    def __init__(self, title=None, first_name=None, last_name=None, is_guest=None,
             company_name=None, mobile=None, landline=None, email=None, password=None,
             fax=None, wants_sms_notification=None, wants_email_notification=None,
             wants_newsletter=None, street_address=None):

        self. title = title
        self.first_name = first_name
        self.last_name = last_name
        self.company_name = company_name
        self.mobile = mobile
        self.landline = landline
        self.email = email
        self.password = password
        self.fax = fax
        self.is_guest = is_guest
        self.wants_sms_notification = wants_sms_notification
        self.wants_email_notification = wants_email_notification
        self.wants_newsletter = wants_newsletter
        self.company_name = company_name
        self.street_address = street_address

Is this construction considered anti-pattern, and if yes, what alternatives do I have?


Solution

  • You could pack the __init__ method's keyword arguments into one dict, and set them dynamically with setattr:

    class User(object):
        GENDER_MALE = 'mr'
        GENDER_FEMALE = 'ms'
        def __init__(self, **kwargs):
            valid_keys = ["title", "first_name", "last_name", "is_guest", "company_name", "mobile", "landline", "email", "password", "fax", "wants_sms_notification", "wants_email_notification", "wants_newsletter","street_address"]
            for key in valid_keys:
                setattr(self, key, kwargs.get(key))
    
    x = User(first_name="Kevin", password="hunter2")
    print(x.first_name, x.password, x.mobile)
    

    However, this has the drawback of disallowing you from supplying arguments without naming them - x = User("Mr", "Kevin") works with your original code, but not with this code.