Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if else statements
#4
[EDITED]
(Sep-14-2018, 10:05 AM)remy Wrote:
class Store:
    Day = 1
    Money = 5
    StoreName = "Lemonade Stand"
    StoreCount = 1
    StoreCost = 3
    StoreProfit = 1.5
    StoreList = []
This code is absolutely redundant - it creates class attributes with the same names as object attributes. It may cause a confusion and is a definite anti-pattern (not class attributes themselves, your usage). See the example below

(Sep-14-2018, 10:05 AM)remy Wrote:
class Store:
    @staticmethod
    def DisplayGameInfo(self):
        print("-----------------------------------")
        print("Day #" + str(self.Day))
        print("Money = ${:0,.2f}".format(self.Money))

    @staticmethod
    def DisplayStoreInfo(self):

        print("Store Name : %s, StoreCount = #%d " % (self.StoreName, self.StoreCount))
        print("-----------------------------------")
Those are wrong cases for staticmethod decorator - those should be regular methods. Staticmethods in Python should not contain self as a parameter - and should not address object/class attributes. The are just independent functions in the class namespace (and are seldom useful).

Take a look at this small experiment that explains my points
Output:
In [15]: class AntiPattern: ...: attr = 1 ...: def __init__(self, attr): ...: self.attr = attr ...: @staticmethod ...: def show_attr(self): ...: return self.attr ...: In [16]: a = AntiPattern(10) In [17]: a.attr Out[17]: 10 In [18]: AntiPattern.attr Out[18]: 1 In [19]: a.show_attr() --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-19-0d2c01c460f9> in <module>() ----> 1 a.show_attr() TypeError: show_attr() missing 1 required positional argument: 'self'
The last - but not the least - your names are un-Pythonic, see PEP-8 for guidance

PS I was wondering why your code worked - here's why, you don't instantiate the class, you use it as is! Your __init__ method is never called. This is an absolutely wrong approach for OOP.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Messages In This Thread
if else statements - by remy - Sep-14-2018, 10:05 AM
RE: if else statements - by Mekire - Sep-14-2018, 10:12 AM
RE: if else statements - by remy - Sep-15-2018, 03:48 AM
RE: if else statements - by volcano63 - Sep-15-2018, 02:51 PM
RE: if else statements - by ichabod801 - Sep-15-2018, 08:09 PM
RE: if else statements - by volcano63 - Sep-15-2018, 08:42 PM
RE: if else statements - by gruntfutuk - Sep-16-2018, 01:17 PM
RE: if else statements - by LinkAiris - Apr-24-2024, 10:22 PM
RE: if else statements - by DeaD_EyE - Apr-25-2024, 07:27 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020