0

If I have a class variable being passed to a function, is there a way that I can get the class that the variable is from?

class someclass:
	def __init__(self):
		self.somevariable='somevalue'

	def somefunction(self):
		print('gotta get here')

def someotherfunction(somevariable):
	somevariable.needparentclasshere.somefunction()

myclass=someclass()
someotherfunction(myclass.somevariable)#this needs to print 'gotta get here'
3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by woooee
0

Only this works:

class someclass:
	def __init__(self):
		self.somevariable='somevalue'

	def somefunction(self):
		print('gotta get here')

def someotherfunction(instance):
	instance.somefunction()

myclass=someclass()
someotherfunction(myclass)#this needs to print 'gotta get here'

as myclass.somevariable is only one string and has nothing connection with someclass. Or must save self in init:

class someclass:
	def __init__(self):
		self.somevariable= self, 'somevalue'

	def somefunction(self):
		print('gotta get here')

def someotherfunction((instance, value)):
	instance.somefunction()

myclass=someclass()
someotherfunction(myclass.somevariable)#this needs to print 'gotta get here'

Edited by pyTony: n/a

1

You perhaps want something along the lines of:

class someclass:
    def __init__(self):
        self.somevariable='somevalue'
     
    def somefunction(self):
        print('gotta get here')
     
def someotherfunction(class_instance):
    print "somevariable -->", class_instance.somevariable

    print "somefunction -->", 
    class_instance.somefunction()
     
myclass=someclass()
someotherfunction(myclass)

Edited by woooee: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.