1

I need to deliver something like this in my program

class the_class_name(Parent):
    the_attribute = self.parent_class_method()
    #the parent class method will return a value
    #but I cannot use self here since there's no self

How can I carry this out? Is there any other alternative that can do the job for me?

I have tried using __init__ like this:

def __init__(self):
    Parent.__init__(self)
    self.attribute = self.the_method()

But then I have problem creating the object, it won't receive any parameters that the Parent class normally receives anymore

martineau
  • 119,623
  • 25
  • 170
  • 301
Stanley Nguyen
  • 442
  • 3
  • 18
  • 2
    What do you want this to do? Your intention is more ambiguous than you might realize. – user2357112 Apr 12 '16 at 16:20
  • I need to refer to a parent class method and I want to assign the returned value to the object attribute right when the object is created – Stanley Nguyen Apr 12 '16 at 16:25
  • What is the implementation of the `Parent` class? – OneCricketeer Apr 12 '16 at 16:26
  • Do you want a reference to that method? In this case: `Parent.parent_class_method`. Or do you want to invoke the method? – tobias_k Apr 12 '16 at 16:26
  • @StanleyNguyen see this: http://stackoverflow.com/questions/805066/call-a-parent-classs-method-from-child-class-in-python – trans1st0r Apr 12 '16 at 16:27
  • @tobias_k I need to invoke the method to get a returned value – Stanley Nguyen Apr 12 '16 at 16:28
  • Do you need to call the Parent class method from inside a method in the derived class (i.e. self available) or a class method from the parent class? The example you showed is using self in the class scope, but it seems from your comment that you understand that problem. Tell us more about what are you trying to do. – Cyb3rFly3r Apr 12 '16 at 16:33
  • @Cyb3rFly3r I want to assign the child class attribute to the returned value without the need of invoking any method inside my child class – Stanley Nguyen Apr 12 '16 at 16:35
  • ..................... – Stanley Nguyen Mar 13 '18 at 15:22

4 Answers4

1

Sounds like you are looking for __init__:

class TheClassName(Parent):
    def __init__(self):
        # Set attribute to the result of the parent method
        self.attribute = super(TheClassName, self).the_method()

EDIT

If your parent class has parameters in it's own __init__ function, include them in the child class:

class Parent(object):
    def __init__(self, foo, bar):
        ...

    @classmethod
    def the_method(cls):
        ...


class TheClassName(Parent):
    def __init__(self, foo, bar):
        super(TheClassName, self).__init__(foo, bar)
        self.attribute = super(TheClassName, self).the_method()

I don't quite understand why you don't just call the parent method on your child object when you need the value though.

Chris Lawlor
  • 47,306
  • 11
  • 48
  • 68
  • I have tried using __init__ like this: 'def __init__(self): Parent.__init__(self) self.attribute = self.the_method()' But then I have problem creating the object, it won't receive any parameters that the Parent class normally receive anymore – Stanley Nguyen Apr 12 '16 at 16:42
0

Use

super(ClassName, self).methodname(arg)

inside a method

def child_method(self, arg):
 super(ClassName, self).methodname(arg)

You cannot use self outside a method.

trans1st0r
  • 2,023
  • 2
  • 17
  • 23
  • I have tried but it gave me an error of "name self is not defined" – Stanley Nguyen Apr 12 '16 at 16:33
  • 1
    `self` is not defined because you are calling it outside a function and the class instance has not been initialized yet. If you tell us more of what you are really trying to accomplish, by showing the parent class as well, we may be able to help. – Cyb3rFly3r Apr 12 '16 at 16:39
0

There is no self at that point of the creation of the subclass, nor is there an instance of the Parent class. That means the only Parent class methods you could call would have to be either static or class methods.

To demonstrate:

class Parent(object):
    @staticmethod
    def static_method():
        return 42

    @classmethod
    def class_method(cls):
        return 43

class TheClassName(Parent):
    the_attribute = Parent.static_method()
    another_attribute = Parent.class_method()

print(TheClassName.the_attribute)  # -> 42
print(TheClassName.another_attribute)  # -> 43
martineau
  • 119,623
  • 25
  • 170
  • 301
0

You must use class methods, declared with the @classmethod decorator, or a @staticmethod. The @classmethod decorator is preferable so that inheritance is handled correctly, i.e. the method is invoked on the derived class (a bit of a technicality, if you are still learning this).

class Alpha(object):
    @classmethod
    def method1(cls):
        return 'method1 has been called on {}'.format(cls)


class Beta(Alpha):
    def __init__(self):
        self.myattr = Beta.method1()


print(Beta().myattr)

method1 has been called on class <'__main__.Beta'>
Cyb3rFly3r
  • 1,321
  • 7
  • 12