2

is there a way to call @staticmethod when using @classmethod

@dataclass class Piza: ingridients: List @classmethod def make_Pizza(cls, ingrid, allerg=False): ingridients = ingrid if allerg: return cls(make_for_allerg(ingrid, allerg)) return cls(ingrid) @staticmethod def make_for_allerg(ingrid, allerg): return ingrid.append(allerg) 

test no allergies:

Piza.make_Pizza(['tomato', 'cheese']) 

Piza(ingridients=['tomato', 'cheese'])

test with allergies false:

Piza.make_Pizza(['tomato', 'cheese'], allerg='pickle') 

--------------------------------------------------------------------------- NameError Traceback (most recent call last) in () 1 ----> 2 Piiza.make_Piza(['tomato', 'cheese'], allerg='pickle')

in make_Pizza(cls, ingrid, allerg) 7 ingridients = ingrid 8 if allerg: ----> 9 return cls(make_for_allerg(ingrid, allerg)) 10 return cls(ingrid) 11

NameError: name 'make_for_allerg' is not defined

1 Answer 1

2

The staticmethod is an attribute of the class object, hence it's still namespaced by the class name. You may call it on the class object:

Piza.make_for_allerg(...) 

Note: Your implementation is not a convincing use-case for a staticmethod. The return value of list.append is always None.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you expand on using Pizza instead of cls to resolve the static method? Not a criticism, just curious.
Not much to say - it will be possible/equivalent to resolve it on cls as well. In this context, Piza and cls will just be different names bound to the same thing (it's a dataclass which is not using a multiple inheritance).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.