How to redefine an expandable class variable?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Alexandr

How to redefine an expandable class variable?
Expamle:

class Asd:
	var variable = 1

class Dsa extends Asd:
    variable = 2
:bust_in_silhouette: Reply From: Xrayez
class Asd:
    var variable = 1

class Dsa extends Asd:
    func _init(p_variable = 2):
        variable = p_variable

# somewhere in your code

var dsa = Dsa.new(2)
print(dsa.variable) # should print 2
  1. class Asd:
    var variable = 1

  2. class Dsa extends Asd:
    func _init(p_variable = 2):
    self.variable = p_variable #it does not carry self?

    somewhere in your code

  3. var dsa = Dsa.new(2) print(dsa.variable) # should print 2

ariel | 2018-11-26 19:17

It’s not necessary in this case. self is needed if you need to distinguish between method’s parameter name and member name:

func _init(variable = 2):
    self.variable = variable  # notice no `p_` prefix

Xrayez | 2018-11-26 19:24