python day 24
Last time, we did a simple example of Python Class , We know a Class works as a container that can store objects with similar features (def). Today we will show a more efficient way to call a Class rather than multiple def functions.
Since I’m really into Hip-hop recently, we will use make an Idol Class as an example!
Python Class Code Example
class Idol(object): def __init__(self, name, age): self.name=name self.age=age def study(self, course): print("%s is practicing %s. " %(self.name, course)) def add_oil(self): if self.age <20: print("%s : 你要努力来见我！U r very important, please treasure your importance <3" %self.name) else: print("%s : 业务能力是别人拿不走的资本！ YAMAHA BLUE..." %self.name) def dance(self): if self.age <20 : print("%s is the Team Leader of the Street Dance of China S1 & 和1000喜一起滑冰 *_*" %self.name) else : print("%s is the Team Leader of the Street Dance of China S3 & 和酷盖一起玩滑板 ^_^" %self.name)
Call Class Old fashion
yibo=Idol("Yibo", 23) yibo.study("Skateboard") yibo.add_oil() yibo.dance()
Yibo is practicing Skateboard. ^_^
jackson=Idol("Jackson Yee 千玺", 19) jackson.study("Painting and Sculpture") yibo.add_oil() jackson.dance()
jackson=Idol("Jackson Yee 千玺", 19) jackson.study("Painting and Sculpture") jackson.add_oil() jackson.dance()
Make a def to call Class
def all1(): id1=Idol("Jackson Yee" , 19 ) id1.study("calligraphy") id1.add_oil() id1.dance() id2=Student("Yibo", 23) id2.study("Lego") id2.add_oil() id2.dance() all1()
Jackson Yee is practicing calligraphy. Jackson Yee : 你要努力来见我! U r very important, please treasure your importance <3 Jackson Yee is the Team Leader of the Street Dance of China S1 & 和1000喜一起滑冰 *_* Yibo is practicing Lego. ^_^
Happy Studying!
I watch a lot of dramas and various TV shows which I am not proud of how I spent my time.
But I m also very proud of myself, that I didn’t just be a fan and watch the stars/idols lives.
I actually practice my headstand/handstand/forearm stand and ran half marathons as 陈意涵 did.
Inspired by 千玺 started to doodle/paint.
now i think there is a great excuse to buy a skateboard because of Yibo!
Although the progress of my python learning is very slow, i am still working on it!
无暇不成玉！
There is no Jade without flaw!
Reference: