unbound method

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
murphy
User
Beiträge: 60
Registriert: Samstag 30. Oktober 2004, 01:34
Wohnort: Berlin
Kontaktdaten:

[vielleicht nur eine blöde frage von einem anfänger]

ich habe wohl nicht verstanden, was "unbound methods" sind.

Code: Alles auswählen

class Foo:
	def __init__(self, name):
		self.name = name
	def think_unbound():
		print "I'm %s and I'm unbound :D yeah it's freedom man" % self.name
	def think(self):
		print "I'm %s and I'm bound :( *sniff*" % self.name
herby = Foo('Herby')
herby.think()  #-> I'm bound :( *sniff*
Foo.think_unbound()  #-> TypeError: unbound method think_unbound() must be called with Foo instance as first argument (got nothing instead)
Foo.think_unbound(herby) #-> TypeError: think_unbound() takes no arguments (1 given)
ich kriege einfach think_unbound() nicht aufgerufen.
wie geht das?
Sorgenkind
User
Beiträge: 34
Registriert: Samstag 24. Juli 2004, 19:25
Kontaktdaten:

Code: Alles auswählen

class Foo: 
    def __init__(self, name): 
        self.name = name
    @staticmethod
    def think_unbound(): 
        print "I'm unbound  yeah it's freedom man but i don't have access to an instance of Foo"
    def think(self): 
        print "I'm %s and I'm bound  *sniff*" % self.name
So in etwa... (Python 2.4)

in Python 2.3 gehts so:

Code: Alles auswählen

class Foo: 
    def __init__(self, name): 
        self.name = name
    def think_unbound(): 
        print "I'm unbound  yeah it's freedom man but i don't have access to an instance of Foo"
    think_unbound=staticmethod(think_unbound)
    def think(self): 
        print "I'm %s and I'm bound  *sniff*" % self.name
Dookie
Python-Forum Veteran
Beiträge: 2010
Registriert: Freitag 11. Oktober 2002, 18:00
Wohnort: Salzburg
Kontaktdaten:

Hi murphy,

eventuell willst du eine staticmethod:

Code: Alles auswählen

class Foo:
    def __init__(self, name):
        self.name = name
    def think_unbound():
        print "I'm %s and I'm unbound Very Happy yeah it's freedom man" % self.name
    think_unbound = staticmethod(think_unbound)
    def think(self):
        print "I'm %s and I'm bound Sad *sniff*" % self.name
herby = Foo('Herby')
herby.think()  #-> I'm bound Sad *sniff*
Foo.think_unbound()  #-> TypeError: unbound method think_unbound() must be called with Foo instance as first argument (got nothing instead)
Foo.think_unbound(herby) #-> TypeError: think_unbound() takes no arguments (1 given)
gibt natürlich auch einen Fehler aus, da ja in einer static-methode kein self definiert ist und du nicht auf self.name zugreiffen kannst.


Gruß

Dookie
[code]#!/usr/bin/env python
import this[/code]
murphy
User
Beiträge: 60
Registriert: Samstag 30. Oktober 2004, 01:34
Wohnort: Berlin
Kontaktdaten:

ok...aber was ist think_unbound ohne @staticmethod? was habe ich da geschrieben? eine mthode, die man nie aufrufen kann?
Dookie
Python-Forum Veteran
Beiträge: 2010
Registriert: Freitag 11. Oktober 2002, 18:00
Wohnort: Salzburg
Kontaktdaten:

murphy hat geschrieben:was habe ich da geschrieben?
Um ehrlich zu sein Misst ;)


Gruß

Dookie
[code]#!/usr/bin/env python
import this[/code]
Antworten