python | Unsorted

Telegram-канал python - Python

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

Subscribe to a channel

Python

Like when you want to group functions. To keep things clean.

Читать полностью…

Python

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

Читать полностью…

Python

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

Читать полностью…

Python

oh so classmethod decorator allows to make methods which can create custom istances of the class?

Читать полностью…

Python

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?

Читать полностью…

Python

And since those two different kinds of method get different arguments, it's crucial to understand the difference between them.

Читать полностью…

Python

That's why

class Foo:
@classmethod
def foo(self):
...

is always wrong. When you have a classmethod, you get a reference to the class, not to an instance. We don't call that self anymore, but cls.

Читать полностью…

Python

cj is right. You need to understand OOP first. You know the difference between a class and an instance?

Читать полностью…

Python

oh, i had no idea, yeah, i really need to study better what decorators does 😅

Читать полностью…

Python

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.

Читать полностью…

Python

and if you want to learn about decorators you can do it separately, no need to mess with OOP on them

Читать полностью…

Python

sell the liver to buy an iPhone now

Читать полностью…

Python

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()

Читать полностью…

Python

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.

Читать полностью…

Python

@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

Читать полностью…

Python

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.

Читать полностью…

Python

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 👍🏻❤️

Читать полностью…

Python

It allows you to get a reference to the class, with which you can create instances. Or do something else with.

Читать полностью…

Python

Right, new creates the instance, but maybe you want a method that creates it with some pre-defined values.

Читать полностью…

Python

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.

Читать полностью…

Python

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.

Читать полностью…

Python

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.

Читать полностью…

Python

Can you send me the link of the Api

Читать полностью…

Python

ok thx

Thank you both, actually. 👍🏻

Читать полностью…

Python

after that, when you have all things clear, you can use/mix them together and you'll understand what @classmethod does

Читать полностью…

Python

🤔 looks like you need to understand OOP concepts first, that's your first task

Читать полностью…

Python

Honestly, idk. I just had my doubts because I don't know decorators very well.

Читать полностью…

Python

why did you think that?

Читать полностью…

Python

the question here is "WHY are you using @classmethod ?"

Читать полностью…

Python

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)

the class in brief

Читать полностью…
Subscribe to a channel