PYLAB Telegram 448
Python_Labs🐍
#سوال آیا در پایتون همانند سایر زبان های شی گرا public - protected و private داریم؟ پاسخ: بله. اما کلمه کلیدی خاصی برای این اعضا در نظر گرفته نشده و با قوانین نامگذاری مشخص میگردد. بدین صورت که _a متغیری از نوع protected و __b (دو تا _ قبل نام) از نوع private…
#سول.
آیا پایتون مثل سایر زبانها سخت گیرانه عمل میکند و واقعا private ها از بیرون کلاس قبال دسترس نیستند؟

خیر! پایتون میگه اگر میخوای دسترسی پیدا کنی با روشی که بهتون میگه بهتون دسترسی میده!!

فرض کنید داریم:

class Employee:
def __init__(self, name, sal):
self.__name=name # private attribute
self.__salary=sal # private attribute

و اگر کلاس بالا را بخواهیم مثل زیر استفاده کنیم
>>> e1=Employee("Bill",10000)

و بخواهیم به فیلد حقوق فرد دسترسی داشته باشیم
>>> e1.__salary

خطای زیر را می بینیم:
AttributeError: 'employee' object has no attribute '__salary'

چرا که فیلد خصوصی تعریف شده! اما میتونید بگید:


>>> e1._Employee__salary
10000

یعنی با ذکر _ و بعد نام کلاس و بعد اسم فیلد دسترسی به عضو خصوصی داد!
حتی میشه عوضش هم کرد!!
>>> e1._Employee__salary=20000
>>> e1._Employee__salary
20000


Python performs name mangling of private variables. Every member with double underscore will be changed to _object._class__variable. If so required, it can still be accessed from outside the class, but the practice should be refrained.



tgoop.com/PyLab/448
Create:
Last Update:

#سول.
آیا پایتون مثل سایر زبانها سخت گیرانه عمل میکند و واقعا private ها از بیرون کلاس قبال دسترس نیستند؟

خیر! پایتون میگه اگر میخوای دسترسی پیدا کنی با روشی که بهتون میگه بهتون دسترسی میده!!

فرض کنید داریم:

class Employee:
def __init__(self, name, sal):
self.__name=name # private attribute
self.__salary=sal # private attribute

و اگر کلاس بالا را بخواهیم مثل زیر استفاده کنیم
>>> e1=Employee("Bill",10000)

و بخواهیم به فیلد حقوق فرد دسترسی داشته باشیم
>>> e1.__salary

خطای زیر را می بینیم:
AttributeError: 'employee' object has no attribute '__salary'

چرا که فیلد خصوصی تعریف شده! اما میتونید بگید:


>>> e1._Employee__salary
10000

یعنی با ذکر _ و بعد نام کلاس و بعد اسم فیلد دسترسی به عضو خصوصی داد!
حتی میشه عوضش هم کرد!!
>>> e1._Employee__salary=20000
>>> e1._Employee__salary
20000


Python performs name mangling of private variables. Every member with double underscore will be changed to _object._class__variable. If so required, it can still be accessed from outside the class, but the practice should be refrained.

BY Python_Labs🐍


Share with your friend now:
tgoop.com/PyLab/448

View MORE
Open in Telegram


Telegram News

Date: |

Telegram is a leading cloud-based instant messages platform. It became popular in recent years for its privacy, speed, voice and video quality, and other unmatched features over its main competitor Whatsapp. Users are more open to new information on workdays rather than weekends. With the administration mulling over limiting access to doxxing groups, a prominent Telegram doxxing group apparently went on a "revenge spree." Just as the Bitcoin turmoil continues, crypto traders have taken to Telegram to voice their feelings. Crypto investors can reduce their anxiety about losses by joining the “Bear Market Screaming Therapy Group” on Telegram. The imprisonment came as Telegram said it was "surprised" by claims that privacy commissioner Ada Chung Lai-ling is seeking to block the messaging app due to doxxing content targeting police and politicians.
from us


Telegram Python_Labs🐍
FROM American