Jump to content

class, static and instance methods in Python - how to use


Recommended Posts


hello dear experts, good evening, 


I was watching a tutorial on class, static and instance methods and they'res 3 things I can't seem to wrap my head around:
When should I use either of the 3 methods?

What does the "__repr__" do?The

example the tutor used does for the code and the project. :

 

class Pizza:
    def __init__(self, ingredients):
        self.ingredients = ingredients

    def __repr__(self):
        return f"Pizza({self.ingredients})"

    @classmethod
    def margherita(cls):
        return cls(['cheese', "tomatoes"])

Pizza(["Pepperoni", "Pinnaple", "ham", "mushrooms"])


How come at the end he was able to fit 3 arguments inside 1 parameter in the __init__ method?

["Pepperoni", "Pinnaple", "ham", "ushrooms"] 

is a single item, a list in this case, being passed onto ingredients.


why he used classmethods and no static methods or instance methods

 

 

well - i really look forward to hear rm you

Quote

What does the "__repr__" do?

 

__repr__ is a special class function named as a shortend version of "representation", that is essentially a to_string equivalent.  It gets called when you try to use an object as if it was a string as well as in some other situations where you want a representation of the object rather than the object itself.  For example, if you execute:

my_pizza = Pizza(["onions", "peppers"])
print(my_pizza)

WIthout a __repr__ or __str__ class function, Python will just print information about the object.

With the __repr__ by convention, the goal is to return an unambiguous representation of the object in a way that the result would be valid Python which could be run again as Python code.  This is why the __repr__ is returning 

Quote

Pizza(["Pepperoni", "Pineapple", "ham", "mushrooms"])

Because that is how the object was created.

 

Quote

How come at the end he was able to fit 3 arguments inside 1 parameter in the __init__ method?

There are not 3 arguments.  There is one argument to the constructor (not including the implicit self), but that argument can be any valid Python type. In the example, the parameter being passed is a list.

Quote

why he used classmethods and no static methods or instance methods

I would assume to help teach you about the repr magic method.  Other types of methods whether static or instance methods have to be explicitly called.  Magic methods are called auto "magically" by Python when it's appropriate.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.