Define different class method depending on whether an input parameter is present/absent

I would like to be able to define a different class method, depending on whether one of the inputs is present or not. Something like:

class mcmc(object):
    def __init__(self, X, Y=None):
        if Y is None:
            def input_vector(self, beta):
                return beta.dot(X)
        else:
            def input_vector(self, beta):
                return beta.dot(np.vstack([X, Y]))

I could have coded this directly in the method, like:

class mcmc(object):
    def __init__(self, X, Y=None):
        self.X = X
        if Y is not None:
            self.Y = Y

    def input_vector(self, beta):
        if self.__hasattr__('Y'):
            return return beta.dot(np.vstack([self.X, self.Y]))
        else:
            return beta.dot(self.X)

but the function is used repeatedly (millions of times), so it probably adds a lot to the execution time.

Another similar possibility, inspired by How can I call any method inside a class depending on an input to another method in the same class?, is probably:

class mcmc(object):
    def __init__(self, X, Y=None):
        self.X = X
        if Y is not None:
            self.Y = Y
            self.method = 'input_vector_XY'
        else:
            self.method = 'input_vector_X'

    def input_vector(self, beta):
        return self.__getattribute__(self.method)(beta)

    def input_vector_XY(self, beta):
        return return beta.dot(np.vstack([self.X, self.Y]))
    
    def input_vector_X(self, beta):
        return beta.dot(self.X)

This also involves an “overhead” in terms of calling an additional function.

10

In order to avoid making the decision each time input_vector is called, instead of doing this on the instance level you could do it on the class level by defining two different classes (maybe with a common base class to avoid duplicating features), and make mcmc a factory function which returns an instance of one or the other class:

def mcmc(X, Y=None):
    if Y is None:
        return MCMCWithoutY(X)
    else:
        return MCMCWithY(X, Y)


class BaseMCMC:
    def __init__(self, X):
        self.X = X

    def common_features(args):
        # do something

    def input_vector(self, beta):
        # must be implemented in derived classes
        # alternatively, provide a default implementation
        raise NotImplementedError


class MCMCWithoutY(BaseMCMC):
    def input_vector(self, beta):
        return beta.dot(self.X)


class MCMCWithY(BaseMCMC):
    def __init__(self, X, Y):
        super().__init__(X)
        self.Y = Y

    def input_vector(self, beta):
        return beta.dot(np.vstack([self.X, self.Y]))

2

The solution that you already mentioned in your question can be improved a bit, and after that your requirements should be satisfied

class mcmc(object):
    def __init__(self, X, Y=None):
        self.X = X
        self.input_vector = self.input_vector_X
        if Y is not None:
            self.Y = Y
            self.input_vector = self.input_vector_XY

    def input_vector_XY(self, beta):
        return beta.dot(np.vstack([self.X, self.Y]))
    
    def input_vector_X(self, beta):
        return beta.dot(self.X)
        
m = mcmc(X=X, Y=None)
m.input_vector(beta)

The downside, is that you won’t receive help from IDE about self.input_vector, or automatic documentation generation, because it is not true method of the class defined by def. It’s just a variable containing reference to the method

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Define different class method depending on whether an input parameter is present/absent

I would like to be able to define a different class method, depending on whether one of the inputs is present or not. Something like:

class mcmc(object):
    def __init__(self, X, Y=None):
        if Y is None:
            def input_vector(self, beta):
                return beta.dot(X)
        else:
            def input_vector(self, beta):
                return beta.dot(np.vstack([X, Y]))

I could have coded this directly in the method, like:

class mcmc(object):
    def __init__(self, X, Y=None):
        self.X = X
        if Y is not None:
            self.Y = Y

    def input_vector(self, beta):
        if self.__hasattr__('Y'):
            return return beta.dot(np.vstack([self.X, self.Y]))
        else:
            return beta.dot(self.X)

but the function is used repeatedly (millions of times), so it probably adds a lot to the execution time.

Another similar possibility, inspired by How can I call any method inside a class depending on an input to another method in the same class?, is probably:

class mcmc(object):
    def __init__(self, X, Y=None):
        self.X = X
        if Y is not None:
            self.Y = Y
            self.method = 'input_vector_XY'
        else:
            self.method = 'input_vector_X'

    def input_vector(self, beta):
        return self.__getattribute__(self.method)(beta)

    def input_vector_XY(self, beta):
        return return beta.dot(np.vstack([self.X, self.Y]))
    
    def input_vector_X(self, beta):
        return beta.dot(self.X)

This also involves an “overhead” in terms of calling an additional function.

10

In order to avoid making the decision each time input_vector is called, instead of doing this on the instance level you could do it on the class level by defining two different classes (maybe with a common base class to avoid duplicating features), and make mcmc a factory function which returns an instance of one or the other class:

def mcmc(X, Y=None):
    if Y is None:
        return MCMCWithoutY(X)
    else:
        return MCMCWithY(X, Y)


class BaseMCMC:
    def __init__(self, X):
        self.X = X

    def common_features(args):
        # do something

    def input_vector(self, beta):
        # must be implemented in derived classes
        # alternatively, provide a default implementation
        raise NotImplementedError


class MCMCWithoutY(BaseMCMC):
    def input_vector(self, beta):
        return beta.dot(self.X)


class MCMCWithY(BaseMCMC):
    def __init__(self, X, Y):
        super().__init__(X)
        self.Y = Y

    def input_vector(self, beta):
        return beta.dot(np.vstack([self.X, self.Y]))

2

The solution that you already mentioned in your question can be improved a bit, and after that your requirements should be satisfied

class mcmc(object):
    def __init__(self, X, Y=None):
        self.X = X
        self.input_vector = self.input_vector_X
        if Y is not None:
            self.Y = Y
            self.input_vector = self.input_vector_XY

    def input_vector_XY(self, beta):
        return beta.dot(np.vstack([self.X, self.Y]))
    
    def input_vector_X(self, beta):
        return beta.dot(self.X)
        
m = mcmc(X=X, Y=None)
m.input_vector(beta)

The downside, is that you won’t receive help from IDE about self.input_vector, or automatic documentation generation, because it is not true method of the class defined by def. It’s just a variable containing reference to the method

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Define different class method depending on whether an input parameter is present/absent

I would like to be able to define a different class method, depending on whether one of the inputs is present or not. Something like:

class mcmc(object):
    def __init__(self, X, Y=None):
        if Y is None:
            def input_vector(self, beta):
                return beta.dot(X)
        else:
            def input_vector(self, beta):
                return beta.dot(np.vstack([X, Y]))

I could have coded this directly in the method, like:

class mcmc(object):
    def __init__(self, X, Y=None):
        self.X = X
        if Y is not None:
            self.Y = Y

    def input_vector(self, beta):
        if self.__hasattr__('Y'):
            return return beta.dot(np.vstack([self.X, self.Y]))
        else:
            return beta.dot(self.X)

but the function is used repeatedly (millions of times), so it probably adds a lot to the execution time.

Another similar possibility, inspired by How can I call any method inside a class depending on an input to another method in the same class?, is probably:

class mcmc(object):
    def __init__(self, X, Y=None):
        self.X = X
        if Y is not None:
            self.Y = Y
            self.method = 'input_vector_XY'
        else:
            self.method = 'input_vector_X'

    def input_vector(self, beta):
        return self.__getattribute__(self.method)(beta)

    def input_vector_XY(self, beta):
        return return beta.dot(np.vstack([self.X, self.Y]))
    
    def input_vector_X(self, beta):
        return beta.dot(self.X)

This also involves an “overhead” in terms of calling an additional function.

10

In order to avoid making the decision each time input_vector is called, instead of doing this on the instance level you could do it on the class level by defining two different classes (maybe with a common base class to avoid duplicating features), and make mcmc a factory function which returns an instance of one or the other class:

def mcmc(X, Y=None):
    if Y is None:
        return MCMCWithoutY(X)
    else:
        return MCMCWithY(X, Y)


class BaseMCMC:
    def __init__(self, X):
        self.X = X

    def common_features(args):
        # do something

    def input_vector(self, beta):
        # must be implemented in derived classes
        # alternatively, provide a default implementation
        raise NotImplementedError


class MCMCWithoutY(BaseMCMC):
    def input_vector(self, beta):
        return beta.dot(self.X)


class MCMCWithY(BaseMCMC):
    def __init__(self, X, Y):
        super().__init__(X)
        self.Y = Y

    def input_vector(self, beta):
        return beta.dot(np.vstack([self.X, self.Y]))

2

The solution that you already mentioned in your question can be improved a bit, and after that your requirements should be satisfied

class mcmc(object):
    def __init__(self, X, Y=None):
        self.X = X
        self.input_vector = self.input_vector_X
        if Y is not None:
            self.Y = Y
            self.input_vector = self.input_vector_XY

    def input_vector_XY(self, beta):
        return beta.dot(np.vstack([self.X, self.Y]))
    
    def input_vector_X(self, beta):
        return beta.dot(self.X)
        
m = mcmc(X=X, Y=None)
m.input_vector(beta)

The downside, is that you won’t receive help from IDE about self.input_vector, or automatic documentation generation, because it is not true method of the class defined by def. It’s just a variable containing reference to the method

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật