نحوه تعریف class method در پایتن
|
2012-02-12, 03:01 PM,
ارسال : #1
|
|||
|
|||
نحوه تعریف class method در پایتن
توی Ruby برای تعریف class method خیلی راحت از کلمه کلیدی self استفاده می کنیم و خیلی راحت هم می شه یه setter/getter برای یه class variable نوشت. برای مثال:
کد : class Foo RMS |
|||
2012-02-12, 09:54 PM,
ارسال : #2
|
|||
|
|||
Re: نحوه تعریف class method در پایتن
در پايتون خيلي سر راست تره :
کد : class Book(object): البته در پايتون نيازي به اين كار ها هم نيست .... اين هم دليلش : <!-- m --><a class="postlink" href="http://blog.fedecarg.com/2008/08/17/no-need-for-setget-methods-in-python/">http://blog.fedecarg.com/2008/08/17/no- ... in-python/</a><!-- m --> Value your freedom or you will lose it, teaches history. |
|||
2012-02-13, 05:50 PM,
ارسال : #3
|
|||
|
|||
Re: نحوه تعریف class method در پایتن
من می خوام یه class method بنویسم نه یه object method این چیزی که شما می گی برای set/get کردن instance variable است .... اين هم دليلش :
http://blog.fedecarg.com/2008/08/17/no-n...in-python/ من چیزی می خوام شبیه این: کد : class Book: کد : Book().get_title() RMS |
|||
2012-03-07, 02:58 PM,
ارسال : #4
|
|||
|
|||
Re: نحوه تعریف class method در پایتن
خوب در واقع پایتون این حالت رو هم به راحتی ساپورت میکنه :
کد : class C: رفرنس خود پایتون کامل توضیح داده : <!-- m --><a class="postlink" href="http://docs.python.org/library/functions.html#staticmethod">http://docs.python.org/library/function ... aticmethod</a><!-- m --> این هم یه نمونه مثال و رفرنس اون : کد : class MyClass(object): <!-- m --><a class="postlink" href="http://stackoverflow.com/questions/735975/static-methods-in-python">http://stackoverflow.com/questions/7359 ... -in-python</a><!-- m --> اما در مورد setter/getter در پایتون راستش خود من هیچ وقت این کار رو نکردم و نمیکنم. این مدل برنامه نویسی بیشتر در دنیای جاوا کاربرد داره تا پایتون (مسایل متدهای پابلیک و ... ). این رو ببینید : نقل قول :Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans. Do not write getters and setters. This is what the ‘property’ built-in is for. And do not take that to mean that you should write getters and setters, and then wrap them in ‘property’. That means that until you prove that you need anything more than a simple attribute access, don’t write getters and setters. They are a waste of CPU time, but more important, they are a waste of programmer time. Not just for the people writing the code and tests, but for the people who have to read and understand them as well.<!-- m --><a class="postlink" href="http://tomayko.com/writings/getters-setters-fuxors">http://tomayko.com/writings/getters-setters-fuxors</a><!-- m --> Value your freedom or you will lose it, teaches history. |
|||
|