in Education by
Can someone please tell me the basic difference between @classmethod and @staticmmethod, I am good with C++ so you can answer with it's reference too. As of now, I only know that @classmethod is used to inherit into sub classes and tells about a class. But I don't know what's the use of that? Why can't we just define a class method without adding any @ attributes like @classmethod or @staticmethod. And when should we use tl;dr:? and why do we use them? and How can we use them? Select the correct answer from above options

1 Answer

0 votes
by
You can use a module function instead of a staticmethod, this method is useless in Python as it knows nothing about the class or instance it was called on. It just gets the arguments that were passed. On the other hand, Classmethod is useful when we want the method to be a factory for the class as It gets actual class it was called on as first argument, Hence this method gets passed the class that it was called on or the instance it was called on as first argument. Therefore @classmethod makes a method whose first argument is the class whereas @staticmethod doesn’t have any implicit arguments. To summon up I am adding an example of both the methods you can try this by your own in Python @classmethod example:- class C: @classmethod Def f(cls, arg1, arg2, arg3,…….argN):….. @Staticmethod example class C: @staticmethod Def f(cls, arg1, arg2, arg3,…….argN):….. Here N denotes all natural No. Hope this helps, Cheers.

Related questions

0 votes
    Can someone tell me a static method in python, I don't want to use initialize a class like this to call ... ClassName.StaticMethod ( ) Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Can someone please tell me the difference between a function decorated with @staticmethod and one with @classmethod? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    What is the meaning of using leading underscores before the name of an object in Python? And what's the difference ... , method, etc.? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    What * and ** do for param2 in the following: def foo(param1, *param2): def bar(param1, **param2): Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I want to write a program which returns different values for different input,it can be easily done using switch ... some alternative? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I check a given object is of a given type? How can I check that a object inherit a given type? There's ... that it's a string? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    In Python What command can I use to find these: Location of file which I am executing, and Current directory ( ... script in Python) Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I generate a string of size N. I want it to be of uppercase English letters and numbers like this: 7I2D86 ... do it in Python. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    There are two codes, what are the difference between them: Using type import types if type(q) is types. ... ): do_something_else() Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Can someone please tell me the difference between _str_ and _repr_ in python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Is there a built-in that removes duplicates from a list in Python, whilst preserving order? I know that I can ... idiom if possible. Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    How can I convert a str from uppercase, or half uppercase to lowercase? E.x. "Decimeter" to "decimeter" Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I get the number of elements of a list? Ex- items = [] items.append("one") items.append("two") items.append("three") Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I am a learner and just want to know the basic concept of super(). Here in the given code I want to know ... () ChildOne() ChildTwo() Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    In a function, How to create and use a global variable? How can I use it in other functions? Is it compulsory ... it in a function? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
...