Python – Is this a bad strategy pattern?

I’ve got a Python project wherein a basic object is created and various different attributes are modified/given to it via what I thought was a good example of a strategy pattern.

In this silly game there are enemies with multiple different potential patterns of movement. The Enemy class has a basic pattern of movement it defaults to; otherwise, a strategy pattern is implemented to randomly choose different ones as the game progresses.

The Enemy’s obj.update() is something like this:

def update(self):
    self.unique_action()
    self.check_if_shooting()
    self.find_new_rect()

So I’d been replacing self.unique_action on the fly with a strategy pattern using what I think are called ‘higher-order functions’.

def new_pattern_of_movement(obj, func):
    def inner(*args, **kwargs):
        ##code for doing different stuff
        ##intentionally NOT returning the function - 
        ##the idea is to replace the default unique_action
    return inner

…
##eventually when generating bad guys for the level…
newEnemy = Enemy()
new_AI = random.choice([new_pattern_of_movement, other_pattern, some_other_pattern]) #etc etc
newEnemy.unique_action = new_AI(newEnemy, newEnemy.unique_action)

After a while, I thought, “self, why not change their attributes as part of the wrapper as well? you change their methods, just set their point_value and graphics and all that dumb stuff too” and I was like “welp I can do that”

def alt_action(obj):
    def inner(*args, **kwargs):
        ##code for their unique_action
        ##again, not returning obj.unique_action
    obj.point_value += 100
    obj.color = colors.YELLOW
    obj.speed -= 1
    return inner

Evidently, this is regarded as “very JavaScript-y”. I find it terribly simple though, and preferable to stringing along a bunch of objects that inherit from a long chain of ancestors.

My job isn’t on the line or anything here, but in such a situation is it really that critical to split these up into separate classes? The first example, at the least, seems fine because it really is just replacing one method with another, but in the second example, I’m definitely redefining object attributes, and I go a bit further than depicted here (replacing the object’s self.draw() methods and that if it’s necessary).

It might be bad, but it’s so stupidly easy.

Is this a good example of how not to implement a strategy pattern?

Does enemy change their behavior post-initialisation ? If not, then your objects are inheriting from a base class, and you shouldn’t implement inheritance with a strategy pattern, for several reasons :

  • Code readability. When you move function pointers around, execution path made less clear. How to know which function will be indeed called, given an instance ?
  • Initialization logic re-use. If you intend to call overriding logic every time you call Enemy(), there is a problem
  • Performance, although this doesn’t matter much

Also, I personally I hope I never have to maintain code that would contain similar patterns than alt_action. Calculating an attribute from a base value is pushing code obscurity and instability to a whole new level, because now, not only correct initialization isn’t guaranteed, but it’s not even idempotent (e.g. initializing twice doesn’t make it initialized correctly…).

In my opinion, you think too much about coding to do the job, while your code is much more than that. Code describe a structure of how things are and interact with each other. Code is also meant to be read and changed more than once. It should make you inclined to choose simple solutions with well-established conventions, over what you find easy.

5

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

Python – Is this a bad strategy pattern?

I’ve got a Python project wherein a basic object is created and various different attributes are modified/given to it via what I thought was a good example of a strategy pattern.

In this silly game there are enemies with multiple different potential patterns of movement. The Enemy class has a basic pattern of movement it defaults to; otherwise, a strategy pattern is implemented to randomly choose different ones as the game progresses.

The Enemy’s obj.update() is something like this:

def update(self):
    self.unique_action()
    self.check_if_shooting()
    self.find_new_rect()

So I’d been replacing self.unique_action on the fly with a strategy pattern using what I think are called ‘higher-order functions’.

def new_pattern_of_movement(obj, func):
    def inner(*args, **kwargs):
        ##code for doing different stuff
        ##intentionally NOT returning the function - 
        ##the idea is to replace the default unique_action
    return inner

…
##eventually when generating bad guys for the level…
newEnemy = Enemy()
new_AI = random.choice([new_pattern_of_movement, other_pattern, some_other_pattern]) #etc etc
newEnemy.unique_action = new_AI(newEnemy, newEnemy.unique_action)

After a while, I thought, “self, why not change their attributes as part of the wrapper as well? you change their methods, just set their point_value and graphics and all that dumb stuff too” and I was like “welp I can do that”

def alt_action(obj):
    def inner(*args, **kwargs):
        ##code for their unique_action
        ##again, not returning obj.unique_action
    obj.point_value += 100
    obj.color = colors.YELLOW
    obj.speed -= 1
    return inner

Evidently, this is regarded as “very JavaScript-y”. I find it terribly simple though, and preferable to stringing along a bunch of objects that inherit from a long chain of ancestors.

My job isn’t on the line or anything here, but in such a situation is it really that critical to split these up into separate classes? The first example, at the least, seems fine because it really is just replacing one method with another, but in the second example, I’m definitely redefining object attributes, and I go a bit further than depicted here (replacing the object’s self.draw() methods and that if it’s necessary).

It might be bad, but it’s so stupidly easy.

Is this a good example of how not to implement a strategy pattern?

Does enemy change their behavior post-initialisation ? If not, then your objects are inheriting from a base class, and you shouldn’t implement inheritance with a strategy pattern, for several reasons :

  • Code readability. When you move function pointers around, execution path made less clear. How to know which function will be indeed called, given an instance ?
  • Initialization logic re-use. If you intend to call overriding logic every time you call Enemy(), there is a problem
  • Performance, although this doesn’t matter much

Also, I personally I hope I never have to maintain code that would contain similar patterns than alt_action. Calculating an attribute from a base value is pushing code obscurity and instability to a whole new level, because now, not only correct initialization isn’t guaranteed, but it’s not even idempotent (e.g. initializing twice doesn’t make it initialized correctly…).

In my opinion, you think too much about coding to do the job, while your code is much more than that. Code describe a structure of how things are and interact with each other. Code is also meant to be read and changed more than once. It should make you inclined to choose simple solutions with well-established conventions, over what you find easy.

5

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

Python – Is this a bad strategy pattern?

I’ve got a Python project wherein a basic object is created and various different attributes are modified/given to it via what I thought was a good example of a strategy pattern.

In this silly game there are enemies with multiple different potential patterns of movement. The Enemy class has a basic pattern of movement it defaults to; otherwise, a strategy pattern is implemented to randomly choose different ones as the game progresses.

The Enemy’s obj.update() is something like this:

def update(self):
    self.unique_action()
    self.check_if_shooting()
    self.find_new_rect()

So I’d been replacing self.unique_action on the fly with a strategy pattern using what I think are called ‘higher-order functions’.

def new_pattern_of_movement(obj, func):
    def inner(*args, **kwargs):
        ##code for doing different stuff
        ##intentionally NOT returning the function - 
        ##the idea is to replace the default unique_action
    return inner

…
##eventually when generating bad guys for the level…
newEnemy = Enemy()
new_AI = random.choice([new_pattern_of_movement, other_pattern, some_other_pattern]) #etc etc
newEnemy.unique_action = new_AI(newEnemy, newEnemy.unique_action)

After a while, I thought, “self, why not change their attributes as part of the wrapper as well? you change their methods, just set their point_value and graphics and all that dumb stuff too” and I was like “welp I can do that”

def alt_action(obj):
    def inner(*args, **kwargs):
        ##code for their unique_action
        ##again, not returning obj.unique_action
    obj.point_value += 100
    obj.color = colors.YELLOW
    obj.speed -= 1
    return inner

Evidently, this is regarded as “very JavaScript-y”. I find it terribly simple though, and preferable to stringing along a bunch of objects that inherit from a long chain of ancestors.

My job isn’t on the line or anything here, but in such a situation is it really that critical to split these up into separate classes? The first example, at the least, seems fine because it really is just replacing one method with another, but in the second example, I’m definitely redefining object attributes, and I go a bit further than depicted here (replacing the object’s self.draw() methods and that if it’s necessary).

It might be bad, but it’s so stupidly easy.

Is this a good example of how not to implement a strategy pattern?

Does enemy change their behavior post-initialisation ? If not, then your objects are inheriting from a base class, and you shouldn’t implement inheritance with a strategy pattern, for several reasons :

  • Code readability. When you move function pointers around, execution path made less clear. How to know which function will be indeed called, given an instance ?
  • Initialization logic re-use. If you intend to call overriding logic every time you call Enemy(), there is a problem
  • Performance, although this doesn’t matter much

Also, I personally I hope I never have to maintain code that would contain similar patterns than alt_action. Calculating an attribute from a base value is pushing code obscurity and instability to a whole new level, because now, not only correct initialization isn’t guaranteed, but it’s not even idempotent (e.g. initializing twice doesn’t make it initialized correctly…).

In my opinion, you think too much about coding to do the job, while your code is much more than that. Code describe a structure of how things are and interact with each other. Code is also meant to be read and changed more than once. It should make you inclined to choose simple solutions with well-established conventions, over what you find easy.

5

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