I want to know if it is possible to call a function using an attribute of a class, for example if I do I className.sort_asc
want to run a function to sort the data in ascending order.
class Numeros():
__init__(self,nums):
self.nums = [nums]
I know that the attributes can be Class or instance, in this case it is instance, so the attribute should be inside the constructor ,__init__
class Numeros():
def __init__(self,nums):
self.nums = [nums]
self.sort = sorted(self.nums)
def add_number(self,n):
self.nums += [n]
However it does not show the expected result
num = Numeros(3)
print(num.nums)
# [3]
num.add_number(5)
print(num.nums)
# [3, 5]
print(num.sort)
# [3] <---- ¿Por que?
Why is this happening? also, can a function be executed with a class attribute?
I think you have a bit of a mess between the following terms:
Difference Between Method and Attribute
Difference between a class attribute and an instance attribute
Instance attributes belong to that instance, that is, we can create other instances from the same class, with different attributes.
On the other hand , class attributes belong to said class and therefore whenever said class is created, it will be initialized with the same class attribute. Which can be changed on all instances, simply by calling the class. following your example
Departure:
Enteros Enteros
Like I said, we can change our class attribute on all instances, just by doing it on the class:
Departure:
Racionales, Racionales
Of course we can also do it on each instance if necessary, but this takes away the meaning of a class attribute
Difference Between Class Method and Instance Method
As with attributes, the difference between class methods and instance methods is that class methods belong to the class, and instance methods belong to the various derived instances of a class. For this explanation I am going to create a new class that will make it easier
We have the class
Hola
, let's see the first method which is a class method. If we call the functionhola()
we have to do it from the class.Output: "hello!"
What happens if we instantiate the class and call the method
hola()
?Departure:
TypeError: hola() takes 0 positional arguments but 1 was given
It gives us an error, because we have said that the method
hola()
belongs to the class, not to the instances . Python internally, when you call a method from an instance, it always inserts a parameter to it which is the instance itself . That's why when you define instance methods you useself
. As our class methodhola()
does not have any arguments in its definition, Python internally passes the instance itself as an argument and obviously gives the errorTypeError: hola() takes 0 positional arguments but 1 was given
because our methodhola()
does not take any arguments.This error happens exactly the opposite if we execute an instance method in a class, it will give us an error saying that we are missing the argument
self
, that is to say that we have defined the method with an argument and we are not passing any to it, you can do the test yourself.Lastly, we have the decorator
@classmethod
that makes it possible for class methods to be called from an instance and from the class, without the above problems occurring.If we run the following you can see where the functions are being called from
Departure:
The solution to your problem.
Once all this has been explained, let's go to your problem. This can be solved in the following way:
Departure:
[1, 2, 3, 4, 5, 7]
Great, you just sort them each time you call the . Although I think what you are referring to is what is known as Instance Properties
Instance Properties
Instance properties is a way of programming our attributes so that each time they are consulted, it is done with a certain process , which is what you are looking for (that is why it seemed convenient to clarify the previous terms up to here). Your example would look like this:
Departure:
[1, 2, 3, 4, 5, 7]
In this case, when
sort
you call you are actually calling an instance property that queries our attributeself._sort
in a certain way, that is, by ordering it. This procedure is "assimilated" (it is not the same) to the way of creating attributes such as Java or C++ in which it is not recommended to access the attributes directly (as is done in Python).Extra bonus
self
is a generic way by convention that exists in Python to refer to the instance of a class, it is not a reserved syntax of the language but it could be anything else, for example "pepito", although it is not recommended, since it would make your code difficult to understand.self._sort
, it is a convention in python (really it is more than a convention, but we are going to leave it here) that tells other programmers "This attribute should not be accessed using it,_sort
you should access using its instance property"