Trying To Call A Function Within Class But It's Not Working
Solution 1:
Classes can contain three types of methods:
Instance methods, e.g.
defimethod(self,...): ...
These can be called only on an instance of a class, and can access class and instance attributes. The first parameter is traditionally called
self
and is automatically passed to the method when called on an instance:instance = Class() instance.imethod(...)
Class methods, e.g.
@classmethoddefcmethod(cls,...): ...
These can be called on the class itself or instances, but can only access class attributes. The first parameter is traditionally named
cls
and is automatically passed to the method when called on a class:Class.cmethod(...) instance.cmethod(...) # works, but can't access instance attributes.
Static methods, e.g.
@staticmethoddefsmethod(...): ...
These are functions related to the class, but can't access instance or class attributes, so they are not passed the class or instance object as the first parameter. They can be called on an instance or a class:
instance = Class() Class.smethod(...) instance.smethod(...)
Example (Python 3.9):
classDemo:
cvar = 1def__init__(self,ivar):
self.ivar = ivar
@classmethoddefcmethod(cls):
print(f'{cls.cvar=}')
@staticmethoddefsmethod():
print('static')
defimethod(self):
print(f'{self.cvar=}{self.ivar=}')
Demo.smethod()
Demo.cmethod()
instance = Demo(2)
instance.smethod()
instance.cmethod()
instance.imethod()
Output:
static
cls.cvar=1static
cls.cvar=1self.cvar=1self.ivar=2
In your case, you defined an instance method, but didn't create an instance first, so the int
parameter you tried to pass was passed as self
, and internally tried to access actions
on the integer 50
. Create an instance first, and don't pass it any parameters. self
will be automatically passed:
compagnie = Compagnie('nom','actions','prix')
compagnie.setActions()
Solution 2:
The class definition defines Compagnie
not Compagnies
. Beyond that, invoking setActions
requires an object so you first need to create:
compagnie = Compagnie("foo", "bar", 33.0)
compagnie.setActions(50)
However, this will fail because your definition of setActions
does not take an argument. From the name, I assume it is meant to look something like:
defsetActions(self, actions):
self.actions = actions
print("Changer le nombre d'actions pour " + self.actions)
Solution 3:
Your method setActions is not set up to take any arguments. "self" is a reference to the instance, and allows you to modify instance variables by using self. inside of that method. To fix this:
defsetActions(self, val):
self.actions = val
print("Changer le nombre d'actions pour " + self.actions)
Solution 4:
defsetActions(self):
print("Changer le nombre d'actions pour " + self.actions)
this is an instance method. It means it accepts the instance as its first argument which is usually denoted by self
. self
allows methods to access the state of the instance. So when you called Compagnie.setActions(50)
, "50" is evaluated as an object. This is how it is evaluated by Python:
defsetActions(50):
print("Changer le nombre d'actions pour " + 50.actions)
since 50 has no property "actions", you get that error. Since it is instance method, it can be called by instance. Or if you want to call it by class, you should create an instance and pass it as an argument:
c=Compagnie(1,"'my_actions'","my_prix") # create an instance
Compagnie.setActions(c) # pass it as an arg
Post a Comment for "Trying To Call A Function Within Class But It's Not Working"