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 1,544 Sep-15-2023, 07:15 PM
Last Post: deanhystad
  Getting names from object dot-syntax acess pedropessoa 2 897 Mar-31-2023, 01:58 PM
Last Post: pedropessoa
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 3,899 Nov-12-2021, 09:55 PM
Last Post: snippsat
  How to pass variables from one class to another hobbyist 18 10,751 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  acess particular element in dataframe using .loc operator. shantanu97 0 1,425 Jun-30-2021, 03:59 AM
Last Post: shantanu97
  Class variables menator01 2 2,013 Jun-04-2020, 04:23 PM
Last Post: Yoriz
  Question about naming variables in class methods sShadowSerpent 1 2,016 Mar-25-2020, 04:51 PM
Last Post: ndc85430
  Understanding Class Variables vindo 9 4,055 Jun-05-2019, 08:04 PM
Last Post: Yoriz
  What is the strategy for working with class variables? AlekseyPython 3 3,016 Feb-24-2019, 05:34 AM
Last Post: AlekseyPython
  Base class variables are not accessible Prabakaran141 3 2,828 Oct-31-2018, 07:13 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020