Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Acess variables from class
#1
Hi I have class like this and staticmethod inside. I want to ask you how to acess self. variables from class ? For example - self.paths

class ImagesLoader: def __init__(self, paths): self.paths = paths @staticmethod def loop(): HERE I WANT ACCESS self.paths
Reply
#2
self is an instance. You have no instance for a static method or a class method.

A static or class method can certainly interact with instances of it's class (or any class for that matter). For example class A has a class method that interacts with instances of the class.
class A(): _instances = [] @classmethod def forall(cls, method, *args): func = getattr(cls, method, None) if func is not None: for x in cls._instances: func(x, *args) def __init__(self, value=0): self.value = value self._instances.append(self) def print(self, *args): print(self, self.value, *args) for i in range(10): A(i) A.forall('print', 'Hello')
forall() works with instances, but it has no instance (no self). It needs to get instances using some other means. In this example instances are added to a list that is a class attribute. For a static method you would need something else. Maybe something as simple ass passing an instance as an argument.
Reply
#3
If you want to use self in a method simply don't make it a static method
class ImagesLoader: def __init__(self, paths): self.paths = paths def loop(self): self.paths
buran likes this post
Reply
#4
A static method is really just a function that happens to be defined within the scope of a class. In the example below the only difference between print_a() defined in A() and the print_a() function is scope, essentially the namespace where the function is defined.
class A(): _instances = [] def __init__(self, value=0): self.value = value @staticmethod def print_a(instance): print(instance.value) def print_a(instance): print(instance.value) a = A('Hello') A.print_a(a) print_a(a)
The static method has no more knowledge or access than the external function. Making the function a static method is mostly a matter of packaging. "Here is a useful function that you can use with this class. I am including it here so it is easy to find."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unchangeable variables in a class? Calab 12 5,338 Sep-15-2023, 07:15 PM
Last Post: deanhystad
  Getting names from object dot-syntax acess pedropessoa 2 2,843 Mar-31-2023, 01:58 PM
Last Post: pedropessoa
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 8,973 Nov-12-2021, 09:55 PM
Last Post: snippsat
  How to pass variables from one class to another hobbyist 18 24,297 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  acess particular element in dataframe using .loc operator. shantanu97 0 2,290 Jun-30-2021, 03:59 AM
Last Post: shantanu97
  Class variables menator01 2 3,344 Jun-04-2020, 04:23 PM
Last Post: Yoriz
  Question about naming variables in class methods sShadowSerpent 1 3,276 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Understanding Class Variables vindo 9 6,881 Jun-05-2019, 08:04 PM
Last Post: Yoriz
  What is the strategy for working with class variables? AlekseyPython 3 4,440 Feb-24-2019, 05:34 AM
Last Post: AlekseyPython
  Base class variables are not accessible Prabakaran141 3 4,375 Oct-31-2018, 07:13 AM
Last Post: buran

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.