135201
You will be muted until you read the rules. Read them! @Rules_for_Python A group about the Python programming language. Offtopic things go here: @pythonofftopic Resources to learn python: @pythonres Group for Hy: @hylang Selenium: @SeleniumPython
Like when you want to group functions. To keep things clean.
Читать полностью…
Mhh ok, so cls is a class reference... but wtf u can make class methods which doesn't use self or cls? this is new for me
Читать полностью…
While self references the specific instance of a class, @classmethod - cls references the class itself
There is also a @staticmethod which is for methods that do not need to use self or cls - they do not need class/instance data but kinda belong there semanticly
oh so classmethod decorator allows to make methods which can create custom istances of the class?
Читать полностью…
oh thx, this is what i asked, i knew that new() method was the instances maker... i knew wrong or i understood wrong what u wrote?
Читать полностью…
And since those two different kinds of method get different arguments, it's crucial to understand the difference between them.
Читать полностью…
That's why
class Foo:
@classmethod
def foo(self):
...
self anymore, but cls.
Читать полностью…
cj is right. You need to understand OOP first. You know the difference between a class and an instance?
Читать полностью…
oh, i had no idea, yeah, i really need to study better what decorators does 😅
Читать полностью…
well classmethod is a built-in, so might be a bit different with decorators in general. decorator itself basically "wrapper for functions" and its for any functions and not only class methods.
Читать полностью…
and if you want to learn about decorators you can do it separately, no need to mess with OOP on them
Читать полностью…
you dont need to use classmethod for your example above. classmethod used when you need a function that related to a class but not related with the class data. eg:
class Person:Читать полностью…
liver_count = 2
@classmethod
def has_liver(cls):
return True
def remove_liver(self):
if self.liver_count == 1: raise PersonNeedALiverError()
self.liver_count -= 1
Person.has_liver() # to use class method
p = Person()
p.remove_liver()
Because, as you can see from my code, the editor methods receive elements from an update method that calls them based on what needs to be done, so I thought they should be preceded by the @classmethod decorator.
Читать полностью…
@Inchidi told me i don't have to use @classmethod but i didn't understood the motivation and so when i should use it and the decorators in general
Читать полностью…
Functions bound to a class that do not need to access the attributes of the instance or which do not need a reference to a class. Basically functions with an association to a class.
Читать полностью…
mhh... i think i understood but i'm not completely sure... can u show me an example if u can? However thx for the help which u are giving me 👍🏻❤️
Читать полностью…
It allows you to get a reference to the class, with which you can create instances. Or do something else with.
Читать полностью…
Right, new creates the instance, but maybe you want a method that creates it with some pre-defined values.
Читать полностью…
Yes, that's what I meant to say, but it's harder for me to say it in English.
I know self and cls, cls is “new” to me because I discovered it not long ago and I will definitely learn more about it.
I know that there is only one class and that from it you can only create (infinite) instances that are saved in variables and therefore become objects (which have all the properties of the class and are instances).
I know that it is not possible to access the keywords of the language and that it is not possible to use keywords as variable names, just as it is not possible to use numbers for the same purpose.
I know the difference between class attributes and class instances: attributes are variables defined within the class and can only be used through instances of the class itself, while instances are variables outside the class that obtain its properties, attributes, and methods.
Because class is reserved keyword in the language. You cannot use it as the name for a variable. Or perhaps you can, but in that case you also shouldn't.
But sometimes you want a method that creates an instance, for example. Such a method will not be called from an instance, but from the class. And it needs a reference to the class. That's what a classmethod is for.
Читать полностью…
after that, when you have all things clear, you can use/mix them together and you'll understand what @classmethod does
🤔 looks like you need to understand OOP concepts first, that's your first task
Читать полностью…
Honestly, idk. I just had my doubts because I don't know decorators very well.
Читать полностью…
the question here is "WHY are you using @classmethod ?"
anyone could link me a guide to use decorators? i don't understand when i should use them
this is my case:
class Foo:
elements = []
@classmethod
def _adder(self, new_elements):
self.elements.extend(new_elements)
@classmethod
def _remover(self, elements):
for e in elements: elements.remove(e) # Actually i don't remember exactly the method
def update_elements(self, elements, action):
match action:
case 'add': self._adder(elements)
case 'rmv': self._remover(elements)