clj-python / libpython-clj

Python bindings for Clojure
Eclipse Public License 2.0
1.06k stars 69 forks source link

how to use create-class #134

Closed PlumpMath closed 3 years ago

PlumpMath commented 3 years ago

I read https://github.com/clj-python/libpython-clj/blob/master/test/libpython_clj/classes_test.clj

and use version clj-python/libpython-clj {:mvn/version "2.00-alpha-5"}

and this py code convert clj

class Person:   
 def __init__(self, name):   
  self.name = name

 def say_hi(self):
  print("hello, my name is", self.name)

p = Person('niki')
p.say_hi()

(def Person (py/create-class "Person" nil {"init" (py/make-tuple-instance-fn (fn [self name] (py/set-attr! self {"name" name}) nil)) "say_hi" (py/make-tuple-instance-fn (fn [self] (bt/print "hello, my name is") ;;; ? print("asdf %s" % (py/$. self name)) (bt/print (py/$. self name))) ;;; ? print( :arg-converter py/as-jvm :method-name "say_hi")}) )


Thanks~!

PlumpMath commented 3 years ago

ah;; solve set-attrs! sorry ^^;;

cnuernber commented 3 years ago

No worries @PlumpMath :-).

cnuernber commented 3 years ago

The create-class pathway is now used heavily in libpython-clj2 all over the place. I will be interested to know if upgrading is an option and repoen if you find any issues.

PlumpMath commented 3 years ago

@cnuernber

(defonce a3py (py/import-module "a3py")) (defonce a3hy (py/import-module "a3hy"))

(defonce imp (py/import-module "imp"))

(defn pyhy-reload [] (py/$a imp reload a3py) (py/$a imp reload a3hy) )

I'm just pythoninit.Create a py module, put it in the site-packages, and call it up from the closure. I've been building classes in closures, and... My hair is going to fall out. kkk; . Maybe... Later, due to libpython-clj. I hope there will be more clarity than hylang.